#![warn(clippy::iter_kv_map)]
#![allow(
    unused_mut,
    clippy::redundant_clone,
    clippy::redundant_closure,
    clippy::suspicious_map,
    clippy::map_identity
)]

use std::collections::{BTreeMap, HashMap};

fn main() {
    let get_key = |(key, _val)| key;
    fn ref_acceptor(v: &u32) -> u32 {
        *v
    }

    let map: HashMap<u32, u32> = HashMap::new();

    let _ = map.keys().collect::<Vec<_>>();
    //~^ iter_kv_map
    let _ = map.values().collect::<Vec<_>>();
    //~^ iter_kv_map
    let _ = map.values().map(|v| v + 2).collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.clone().into_keys().collect::<Vec<_>>();
    //~^ iter_kv_map
    let _ = map.clone().into_keys().map(|key| key + 2).collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.clone().into_values().collect::<Vec<_>>();
    //~^ iter_kv_map
    let _ = map.clone().into_values().map(|val| val + 2).collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.clone().values().collect::<Vec<_>>();
    //~^ iter_kv_map
    let _ = map.keys().filter(|x| x.is_multiple_of(2)).count();
    //~^ iter_kv_map

    // Don't lint
    let _ = map
        .iter()
        .filter(|(_, val)| val.is_multiple_of(2))
        .map(|(key, _)| key)
        .count();
    let _ = map.iter().map(get_key).collect::<Vec<_>>();

    // Linting the following could be an improvement to the lint
    // map.iter().filter_map(|(_, val)| (val.is_multiple_of(2)).then(val * 17)).count();

    // Lint
    let _ = map.keys().map(|key| key * 9).count();
    //~^ iter_kv_map
    let _ = map.values().map(|value| value * 17).count();
    //~^ iter_kv_map

    // Preserve the ref in the fix.
    let _ = map.clone().into_values().map(|ref val| ref_acceptor(val)).count();
    //~^ iter_kv_map

    // Preserve the mut in the fix.
    let _ = map
        //~^ iter_kv_map
        .clone().into_values().map(|mut val| {
            val += 2;
            val
        })
        .count();

    // Don't let a mut interfere.
    let _ = map.clone().into_values().count();
    //~^ iter_kv_map

    let map: BTreeMap<u32, u32> = BTreeMap::new();

    let _ = map.keys().collect::<Vec<_>>();
    //~^ iter_kv_map
    let _ = map.values().collect::<Vec<_>>();
    //~^ iter_kv_map
    let _ = map.values().map(|v| v + 2).collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.clone().into_keys().collect::<Vec<_>>();
    //~^ iter_kv_map
    let _ = map.clone().into_keys().map(|key| key + 2).collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.clone().into_values().collect::<Vec<_>>();
    //~^ iter_kv_map
    let _ = map.clone().into_values().map(|val| val + 2).collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.clone().values().collect::<Vec<_>>();
    //~^ iter_kv_map
    let _ = map.keys().filter(|x| x.is_multiple_of(2)).count();
    //~^ iter_kv_map

    // Don't lint
    let _ = map
        .iter()
        .filter(|(_, val)| val.is_multiple_of(2))
        .map(|(key, _)| key)
        .count();
    let _ = map.iter().map(get_key).collect::<Vec<_>>();

    // Linting the following could be an improvement to the lint
    // map.iter().filter_map(|(_, val)| (val.is_multiple_of(2)).then(val * 17)).count();

    // Lint
    let _ = map.keys().map(|key| key * 9).count();
    //~^ iter_kv_map
    let _ = map.values().map(|value| value * 17).count();
    //~^ iter_kv_map

    // Preserve the ref in the fix.
    let _ = map.clone().into_values().map(|ref val| ref_acceptor(val)).count();
    //~^ iter_kv_map

    // Preserve the mut in the fix.
    let _ = map
        //~^ iter_kv_map
        .clone().into_values().map(|mut val| {
            val += 2;
            val
        })
        .count();

    // Don't let a mut interfere.
    let _ = map.clone().into_values().count();
    //~^ iter_kv_map
}

#[clippy::msrv = "1.53"]
fn msrv_1_53() {
    let map: HashMap<u32, u32> = HashMap::new();

    // Don't lint because into_iter is not supported
    let _ = map.clone().into_iter().map(|(key, _)| key).collect::<Vec<_>>();
    let _ = map.clone().into_iter().map(|(key, _)| key + 2).collect::<Vec<_>>();

    let _ = map.clone().into_iter().map(|(_, val)| val).collect::<Vec<_>>();
    let _ = map.clone().into_iter().map(|(_, val)| val + 2).collect::<Vec<_>>();

    // Lint
    let _ = map.keys().collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.values().collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.values().map(|v| v + 2).collect::<Vec<_>>();
    //~^ iter_kv_map
}

#[clippy::msrv = "1.54"]
fn msrv_1_54() {
    // Lint all
    let map: HashMap<u32, u32> = HashMap::new();

    let _ = map.clone().into_keys().collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.clone().into_keys().map(|key| key + 2).collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.clone().into_values().collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.clone().into_values().map(|val| val + 2).collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.keys().collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.values().collect::<Vec<_>>();
    //~^ iter_kv_map

    let _ = map.values().map(|v| v + 2).collect::<Vec<_>>();
    //~^ iter_kv_map
}

fn issue14595() {
    pub struct Foo(BTreeMap<String, i32>);

    impl AsRef<BTreeMap<String, i32>> for Foo {
        fn as_ref(&self) -> &BTreeMap<String, i32> {
            &self.0
        }
    }

    let map = Foo(BTreeMap::default());

    let _ = map.as_ref().values().copied().collect::<Vec<_>>();
    //~^ iter_kv_map
}

fn issue16340() {
    let hm: HashMap<&str, &str> = HashMap::new();
    let _ = hm.keys().map(|key| vec![key]);
    //~^ iter_kv_map
}

fn issue16515() {
    let hash_map: HashMap<u32, u32> = HashMap::new();
    hash_map.keys().flat_map(|k| Some(*k));
    //~^ iter_kv_map

    hash_map.values().flat_map(|v| Some(*v));
    //~^ iter_kv_map

    hash_map.keys().filter_map(|k| (k > &0).then_some(1));
    //~^ iter_kv_map

    hash_map.values().filter_map(|v| (v > &0).then_some(1));
    //~^ iter_kv_map

    hash_map.into_keys().flat_map(|k| Some(k));
    //~^ iter_kv_map

    let hash_map: HashMap<u32, u32> = HashMap::new();
    hash_map.into_values().flat_map(|v| Some(v));
    //~^ iter_kv_map

    let hash_map: HashMap<u32, u32> = HashMap::new();
    hash_map.into_keys().filter_map(|k| (k > 0).then_some(1));
    //~^ iter_kv_map

    let hash_map: HashMap<u32, u32> = HashMap::new();
    hash_map.into_values().filter_map(|v| (v > 0).then_some(1));
    //~^ iter_kv_map
}

fn issue16742() {
    let map: HashMap<u32, Vec<u32>> = HashMap::new();
    map.values().flat_map(|v| v.iter().map(|i| *i + 1));
    //~^ iter_kv_map
    map.values().flatten();
    //~^ iter_kv_map

    let map: HashMap<u32, Vec<u32>> = HashMap::new();
    map.into_values().flatten();
    //~^ iter_kv_map
}
