Add single_option_map lint
This commit is contained in:
parent
390286d664
commit
4d4ef0000c
6 changed files with 201 additions and 0 deletions
69
tests/ui/single_option_map.rs
Normal file
69
tests/ui/single_option_map.rs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#![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> {
|
||||
//~^ ERROR: `fn` that only maps over argument
|
||||
arg.map(|x| x * 2)
|
||||
}
|
||||
|
||||
fn j(arg: Option<u64>) -> Option<u64> {
|
||||
//~^ ERROR: `fn` that only maps over argument
|
||||
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> {
|
||||
//~^ ERROR: `fn` that only maps over argument
|
||||
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();
|
||||
}
|
||||
37
tests/ui/single_option_map.stderr
Normal file
37
tests/ui/single_option_map.stderr
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
error: `fn` that only maps over argument
|
||||
--> tests/ui/single_option_map.rs:8:1
|
||||
|
|
||||
LL | / fn h(arg: Option<u32>) -> Option<u32> {
|
||||
LL | |
|
||||
LL | | arg.map(|x| x * 2)
|
||||
LL | | }
|
||||
| |_^
|
||||
|
|
||||
= help: move the `.map` to the caller or to an `_opt` function
|
||||
= note: `-D clippy::single-option-map` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::single_option_map)]`
|
||||
|
||||
error: `fn` that only maps over argument
|
||||
--> tests/ui/single_option_map.rs:13:1
|
||||
|
|
||||
LL | / fn j(arg: Option<u64>) -> Option<u64> {
|
||||
LL | |
|
||||
LL | | arg.map(|x| x * 2)
|
||||
LL | | }
|
||||
| |_^
|
||||
|
|
||||
= help: move the `.map` to the caller or to an `_opt` function
|
||||
|
||||
error: `fn` that only maps over argument
|
||||
--> tests/ui/single_option_map.rs:22:1
|
||||
|
|
||||
LL | / fn mul_args_opt(a: Option<String>, b: u64) -> Option<String> {
|
||||
LL | |
|
||||
LL | | a.map(|val| mul_args(val, b + 1))
|
||||
LL | | }
|
||||
| |_^
|
||||
|
|
||||
= help: move the `.map` to the caller or to an `_opt` function
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue