about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/entry_unfixable.rs
blob: c4c055572086eb0fc4ec863592372b5ad0e7a7c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#![allow(clippy::needless_pass_by_value, clippy::collapsible_if)]
#![warn(clippy::map_entry)]

use std::collections::HashMap;
use std::hash::Hash;

macro_rules! m {
    ($e:expr) => {{ $e }};
}

macro_rules! insert {
    ($map:expr, $key:expr, $val:expr) => {
        $map.insert($key, $val)
    };
}

mod issue13306 {
    use std::collections::HashMap;

    struct Env {
        enclosing: Option<Box<Env>>,
        values: HashMap<String, usize>,
    }

    impl Env {
        fn assign(&mut self, name: String, value: usize) -> bool {
            if !self.values.contains_key(&name) {
                //~^ map_entry
                self.values.insert(name, value);
                true
            } else if let Some(enclosing) = &mut self.enclosing {
                enclosing.assign(name, value)
            } else {
                false
            }
        }
    }
}

fn issue9925(mut hm: HashMap<String, bool>) {
    let key = "hello".to_string();
    if hm.contains_key(&key) {
        //~^ map_entry
        let bval = hm.get_mut(&key).unwrap();
        *bval = false;
    } else {
        hm.insert(key, true);
    }
}

mod issue9470 {
    use std::collections::HashMap;
    use std::sync::Mutex;

    struct Interner(i32);

    impl Interner {
        const fn new() -> Self {
            Interner(0)
        }

        fn resolve(&self, name: String) -> Option<String> {
            todo!()
        }
    }

    static INTERNER: Mutex<Interner> = Mutex::new(Interner::new());

    struct VM {
        stack: Vec<i32>,
        globals: HashMap<String, i32>,
    }

    impl VM {
        fn stack_top(&self) -> &i32 {
            self.stack.last().unwrap()
        }

        fn resolve(&mut self, name: String, value: i32) -> Result<(), String> {
            if self.globals.contains_key(&name) {
                //~^ map_entry
                self.globals.insert(name, value);
            } else {
                let interner = INTERNER.lock().unwrap();
                return Err(interner.resolve(name).unwrap().to_owned());
            }

            Ok(())
        }
    }
}

fn main() {}