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

Copy link
Contributor

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case should be added, to reflect one problem the lint had in its earlier form:

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

Also, wrapping a function can be done more explicitly than with just the function name:

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

It would be great not to lint here too. But it can always be done later if this proves cumbersome.

tests/ui/single_option_map.rs Show resolved Hide resolved
tests/ui/single_option_map.rs Show resolved Hide resolved
yusufraji and others added 2 commits January 28, 2025 22:05
This ensures lints aren't accidentally lost during future refactoring, as they will be checked by uitest

Co-authored-by: Samuel Tardieu <[email protected]>
Co-authored-by: Samuel Tardieu <[email protected]>
@yusufraji
Copy link
Contributor Author

This test case should be added, to reflect one problem the lint had in its earlier form:

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

Also, wrapping a function can be done more explicitly than with just the function name:

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

It would be great not to lint here too. But it can always be done later if this proves cumbersome.

Thanks, I've added it.

maps_static_option().map(|_| arg.unwrap())
}

// No lint: wrapper function with η-expanded form
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it lints in the current case (look at the stderr file).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I just noticed that. For future reference, is there a way to change status of the PR to WIP, in case I find a problem I urgently need to resolve.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have two ways of doing this:

  • Use rustbot with "@rustbot author" to indicate that your PR waits for an action on your side, and "@rustbot review" when you want to switch it back to ready to review.
  • Using the GitHub interface, you can at any point set the PR to "Draft", then set it back to "Ready" when you're done. This has the disadvantage of not changing the status in the TriageBot dashboard AFAICT.

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