-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add single_option_map
lint
#14033
base: master
Are you sure you want to change the base?
Add single_option_map
lint
#14033
Conversation
r? @Manishearth rustbot has assigned @Manishearth. Use |
r? @llogiq |
It is also necessary to check that the receiver of fn f(x: &str) -> Option<u32> {
x.parse::<u32>().ok().map(|x| x+1)
} |
da10dc2
to
609541b
Compare
&& method_name.ident.name == sym::map | ||
&& let callee_type = cx.typeck_results().expr_ty(callee) | ||
&& is_type_diagnostic_item(cx, callee_type, sym::Option) | ||
&& let ExprKind::Path(_path) = callee.kind |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably make sure that this path references an argument. Otherwise we risk linting
static MAYBE_ATOMIC: Option<AtomicUsize> = Some(AtomicUsize::new());
fn maps_static_option() -> Option<usize> {
MAYBE_ATOMIC.as_ref().map(|a| a.load(Ordering::Relaxed))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been added. Thanks for the help.
@@ -0,0 +1,17 @@ | |||
#![warn(clippy::single_option_map)] | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the next thing we'll want to ensure is that we don't lint on functions that just wrap other functions to do the optional thing, because that is actually a good API.
e.g. we don't want to lint
fn manipulate(i: i32) -> i32 { i + 1 }
fn manipulate_opt(opt_i: Option<i32>) -> Option<i32> { opt_i.map(manipulate) }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this to the test, and tests didn't fail. I might be missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the lint never runs, it is missing a
store.register_late_pass(|_| Box::new(single_option_map::SingleOptionMap));
near the end of clippy_lints/src/lib.rs
.
This is also probably why the .fixed
and .stderr
files are missing from the PR.
f89506e
to
4df6872
Compare
Checks for functions with method calls to
.map(_)
on an arg of typeOption
as the outermost expression.Fixes #774