Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

yusufraji
Copy link
Contributor

Checks for functions with method calls to .map(_) on an arg of type Option as the outermost expression.

Fixes #774

changelog: [`single_option_map`]: Checks for functions with method calls to `.map(_)` on an arg of type `Option` as the outermost expression.

@rustbot
Copy link
Collaborator

rustbot commented Jan 19, 2025

r? @Manishearth

rustbot has assigned @Manishearth.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 19, 2025
@yusufraji
Copy link
Contributor Author

r? @llogiq

@rustbot rustbot assigned llogiq and unassigned Manishearth Jan 19, 2025
@samueltardieu
Copy link
Contributor

It is also necessary to check that the receiver of map() is the function parameter. Otherwise, the lint will trigger on code such as:

fn f(x: &str) -> Option<u32> {
    x.parse::<u32>().ok().map(|x| x+1)
}

&& 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
Copy link
Contributor

@llogiq llogiq Jan 21, 2025

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))
}

Copy link
Contributor Author

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)]

Copy link
Contributor

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) }

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lint functions that map over option arg
5 participants