Skip to content

New lint: mem_replace_option_with_some #14197

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

Merged
merged 2 commits into from
Feb 12, 2025

Conversation

samueltardieu
Copy link
Contributor

mem::replace(opt, Some(v)) can be replaced by opt.replace(v).

Close #14195

changelog: [mem_replace_option_with_some]: new lint

@rustbot
Copy link
Collaborator

rustbot commented Feb 11, 2025

r? @llogiq

rustbot has assigned @llogiq.
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 Feb 11, 2025
@samueltardieu
Copy link
Contributor Author

Hits from lintcheck are legitimate.

@@ -238,6 +283,13 @@ impl<'tcx> LateLintPass<'tcx> for MemReplace {
} else if self.msrv.meets(msrvs::MEM_TAKE) && is_expr_used_or_unified(cx.tcx, expr) {
check_replace_with_default(cx, src, dest, expr.span);
}
// Check that second argument is `Option::Some`
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't that be part of the check method? To me as a reviewer, this splits the code and if I look at the check method in isolation, it's not clear what actually is checked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally. I have also reorganized the other checks so that they are more self-contained, in the first commit.

///
/// ### Example
/// ```no_run
/// use std::mem;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is only used once, so we can inline the std:: and remove the import to shorten the example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Make each lint be more self-contained, and trigger only the next lint
when the previous one didn't already rewrite the expression.
@samueltardieu samueltardieu force-pushed the push-qyyrwsvsuvyw branch 2 times, most recently from 538b71d to 86796f2 Compare February 12, 2025 16:37
@samueltardieu samueltardieu requested a review from llogiq February 12, 2025 17:17
`mem::replace(opt, Some(v))` can be replaced by `opt.replace(v)`.
@llogiq
Copy link
Contributor

llogiq commented Feb 12, 2025

Needs a cargo dev fmt, otherwise this is merge-worthy.

@samueltardieu
Copy link
Contributor Author

Resubmitted with reformatted comment as the only change.

@llogiq llogiq enabled auto-merge February 12, 2025 18:58
@llogiq llogiq added this pull request to the merge queue Feb 12, 2025
Merged via the queue into rust-lang:master with commit 4129f5c Feb 12, 2025
11 checks passed
@samueltardieu samueltardieu deleted the push-qyyrwsvsuvyw branch February 12, 2025 20:58
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.

New lint: mem_replace_option_with_some
3 participants