about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/single_option_map.rs
blob: 6126582f4635a44ef0efacfcc38525df6cdc587f (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
#![warn(clippy::single_option_map)]

use std::sync::atomic::{AtomicUsize, Ordering};

static ATOM: AtomicUsize = AtomicUsize::new(42);
static MAYBE_ATOMIC: Option<&AtomicUsize> = Some(&ATOM);

fn h(arg: Option<u32>) -> Option<u32> {
    //~^ single_option_map

    arg.map(|x| x * 2)
}

fn j(arg: Option<u64>) -> Option<u64> {
    //~^ single_option_map

    arg.map(|x| x * 2)
}

fn mul_args(a: String, b: u64) -> String {
    a
}

fn mul_args_opt(a: Option<String>, b: u64) -> Option<String> {
    //~^ single_option_map

    a.map(|val| mul_args(val, b + 1))
}

// No lint: no `Option` argument argument
fn maps_static_option() -> Option<usize> {
    MAYBE_ATOMIC.map(|a| a.load(Ordering::Relaxed))
}

// No lint: wrapped by another function
fn manipulate(i: i32) -> i32 {
    i + 1
}
// No lint: wraps another function to do the optional thing
fn manipulate_opt(opt_i: Option<i32>) -> Option<i32> {
    opt_i.map(manipulate)
}

// No lint: maps other than the receiver
fn map_not_arg(arg: Option<u32>) -> Option<u32> {
    maps_static_option().map(|_| arg.unwrap())
}

// No lint: wrapper function with η-expanded form
#[allow(clippy::redundant_closure)]
fn manipulate_opt_explicit(opt_i: Option<i32>) -> Option<i32> {
    opt_i.map(|x| manipulate(x))
}

// No lint
fn multi_args(a: String, b: bool, c: u64) -> String {
    a
}

// No lint: contains only map of a closure that binds other arguments
fn multi_args_opt(a: Option<String>, b: bool, c: u64) -> Option<String> {
    a.map(|a| multi_args(a, b, c))
}

fn main() {
    let answer = Some(42u32);
    let h_result = h(answer);

    let answer = Some(42u64);
    let j_result = j(answer);
    maps_static_option();
}