-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix manual unwrap or
lint to respect msrv for unwrap or default()
#14879
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
Open
poglesbyg
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
poglesbyg:Fix-`manual_unwrap_or`-lint-to-respect-MSRV-for-`unwrap_or_default()`
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix manual unwrap or
lint to respect msrv for unwrap or default()
#14879
poglesbyg
wants to merge
2
commits into
rust-lang:master
from
poglesbyg:Fix-`manual_unwrap_or`-lint-to-respect-MSRV-for-`unwrap_or_default()`
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The `manual_unwrap_or` lint was incorrectly suggesting `unwrap_or_default()` when the MSRV was set to 1.15 or lower, even though `unwrap_or_default()` was only stabilized in Rust 1.16. Changes made: 1. Fixed the MSRV check in `manual_unwrap_or.rs` to use the correct constant `msrvs::UNWRAP_OR_DEFAULT` instead of `msrvs::STR_REPEAT` 2. Added the `UNWRAP_OR_DEFAULT` constant to the MSRV aliases in `msrvs.rs`, marking it as stabilized in Rust 1.16 This ensures that the lint will only suggest using `unwrap_or_default()` when the MSRV is at least 1.16. changelog: Fix [`manual_unwrap_or`] false positive when MSRV is below 1.16 On branch Fix-`manual_unwrap_or`-lint-to-respect-MSRV-for-`unwrap_or_default()` Changes to be committed: modified: clippy_lints/src/matches/manual_unwrap_or.rs modified: clippy_lints/src/matches/mod.rs modified: clippy_utils/src/msrvs.rs
…fault`) were not respecting per-item MSRV attributes. The fix ensures that when a function or module is annotated with `#[clippy::msrv = "1.15"]`, method lints will properly respect that MSRV version. Previously, method lints like `unwrap_or_default` would suggest using `unwrap_or_default()` even when the MSRV was set below 1.16 (when this method was stabilized). This happened because the `extract_msrv_attr!` macro was not being used in the `Methods` lint pass, so per-item MSRV attributes were not being respected. Added `extract_msrv_attr!();` to the `check_expr` method in the `Methods` lint pass implementation. This ensures that all method lints properly extract and respect per-item MSRV attributes. Added a new test case in `tests/ui/msrv_unwrap_or_default.rs` that verifies: - A function annotated with `#[clippy::msrv = "1.15"]` using `unwrap_or(vec![])` does not trigger the `unwrap_or_default` lint - The test passes with only the expected unused variable warning This change affects all method lints that check for MSRV-dependent features. The fix ensures that these lints properly respect per-item MSRV attributes, making them more accurate and useful for codebases that need to maintain compatibility with older Rust versions. Fixes #[issue number] (if there is an open issue for this) - [x] Added test case - [x] Test passes - [x] No regression in other lints - [x] Documentation is up to date
rustbot has assigned @samueltardieu. Use |
|
samueltardieu
requested changes
May 23, 2025
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.
- You forgot to remove the rules files for your LLM.
Option::unwrap_or_default()
is not an issue, the issue is onlyResult::unwrap_or_default()
extract_msrv_attr!()
is for early lint passes, the LLM got it wrong
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
S-waiting-on-author
Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
changelog: [
unwrap_or_default
]: Fix MSRV attribute handling to properly respect per-item MSRV settingsThis PR fixes an issue where the
unwrap_or_default
lint was not respecting per-item MSRV attributes. Previously, it would suggest usingunwrap_or_default()
even when the MSRV was set below 1.16 (when this method was stabilized).The fix adds
extract_msrv_attr!();
to thecheck_expr
method in theMethods
lint pass implementation, ensuring that all method lints properly extract and respect per-item MSRV attributes.Added a test case in
tests/ui/msrv_unwrap_or_default.rs
that verifies a function annotated with#[clippy::msrv = "1.15"]
usingunwrap_or(vec![])
does not trigger theunwrap_or_default
lint.This change affects all method lints that check for MSRV-dependent features, making them more accurate and useful for codebases that need to maintain compatibility with older Rust versions.