-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Uplift clippy::option_env_unwrap
lint
#111738
Conversation
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
☔ The latest upstream changes (presumably #111345) made this pull request unmergeable. Please resolve the merge conflicts. |
142cf91
to
408828c
Compare
☔ The latest upstream changes (presumably #111799) made this pull request unmergeable. Please resolve the merge conflicts. |
@rfcbot fcp close We discussed this in the lang-team meeting and felt it did not meet the rustc bar of preventing bugs or helping to shape ecosystem wide consistency (like naming conventions). We would be interested in some kind of "custom lint" mechanism. Ideally this would be a pattern-matching scheme that would ecosystem crates provide this sort of lint. A more limited thing might be something like The motivation here is that we think "usage lints" like this add value, but to do so, you need an awful lot of them, and we think the best way to get that is to let people add them themselves. Otherwise, clippy is a better home. |
This comment was marked as outdated.
This comment was marked as outdated.
@nikomatsakis proposal cancelled. |
@rfcbot fcp close We discussed this in the lang-team meeting and felt it did not meet the rustc bar of preventing bugs or helping to shape ecosystem wide consistency (like naming conventions). We would be interested in some kind of "custom lint" mechanism. Ideally this would be a pattern-matching scheme that would ecosystem crates provide this sort of lint. A more limited thing might be something like #[must_use] (e.g., #[prefer_on_unwrap("env!")], but some members of the team were skeptical of such a narrow purpose attribute. Regardless that would be a separate proposal that would ultimately require an RFC. The motivation here is that we think "usage lints" like this add value, but to do so, you need an awful lot of them, and we think the best way to get that is to let people add them themselves. Otherwise, clippy is a better home. |
Team member @nikomatsakis has proposed to close this. The next step is review by the rest of the tagged team members: Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
Agreed that "you could write this in a better way" lints -- particularly about specific functions rather than language constructs -- are a better fit for a more stylistically inclined tool (like clippy!) rather than natively in rustc itself. @rfcbot reviewed Clippy has lots of helpful lints about specific things that I don't think I'd want to bring into rustc. My go-to example is |
While I agree with the arguments presented, I would just like to mention that the changes proposed by this lint are not stylistics! Using |
Given that @nikomatsakis originally started the FCP to close, with the motivation "did not meet the rustc bar of preventing bugs", and given the point @Urgau made and @nikomatsakis supported that this is in fact catching something that's almost always a bug, I'm going to cancel the FCP to close, and start an FCP to merge to see if we have consensus in that direction. (Eagerly awaiting the rustbot or rfcbot functionality for tracking statuses by person and not predisposing in one direction or the other.) |
@joshtriplett proposal cancelled. |
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
FWIW, there are rare situations where this lint is a false positive. So maybe this shouldn't be called |
Do you have an example where someone might legitimately prefer to panic at run-time instead of having a compile-time error for a compile-time environment variable? |
Yes, we used to have such code in Miri. Basically for the same reason that people use In the Miri case the situation was of the sort "if env var if let Some(a) = option_env!("A") {
// ...
} else {
let b = option_env!("B").unwrap();
// ...
} Using |
Interesting example. The minimized code is indeed an issue, and the wording should be soften. Concerning the lint name, what about |
|
As a drive-by comment I personally was thinking of the same situation that @RalfJung brought up and given that the lint can have false positives I would personally say it shouldn't be an on-by-default lint. I know I feel differently about lints than many others, though, but my thinking is that rustc lints should never have false positives in general. |
@joshtriplett proposal cancelled. |
Team member @joshtriplett has proposed to close this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
That's not necessarily the case. Panics in consteval contexts still become compilation errors, and in some of codebases I work on I and my team had converged towards using the pattern like (But my comment is just a more specific example of “this is a style lint” that you were responding to.) |
The final comment period, with a disposition to close, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. |
This PR aims at uplifting the
clippy::option_env_unwrap
lint into rustc.incorrect_option_env_unwraps
(warn-by-default)
The
incorrect_option_env_unwraps
lint checks for usage ofoption_env!(...).unwrap()
and suggests using theenv!
macro instead.Example
Explanation
Unwrapping the result of
option_env!
will panic at run-time if the environment variable doesn't exist, whereasenv!
catches it at compile-time.Mostly followed the instructions for uplifting a clippy lint described here: #99696 (review)
@rustbot label: +I-lang-nominated
r? compiler