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

Uplift clippy::option_env_unwrap lint #111738

Closed
wants to merge 4 commits into from

Conversation

Urgau
Copy link
Member

@Urgau Urgau commented May 18, 2023

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 of option_env!(...).unwrap() and suggests using the env! macro instead.

Example

let _ = option_env!("HOME").unwrap();

Explanation

Unwrapping the result of option_env! will panic at run-time if the environment variable doesn't exist, whereas env! 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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 18, 2023
@rustbot
Copy link
Collaborator

rustbot commented May 18, 2023

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added the I-lang-nominated Nominated for discussion during a lang team meeting. label May 18, 2023
@bors
Copy link
Contributor

bors commented May 19, 2023

☔ The latest upstream changes (presumably #111345) made this pull request unmergeable. Please resolve the merge conflicts.

@Urgau Urgau force-pushed the uplift_option_env_unwrap branch from 142cf91 to 408828c Compare May 19, 2023 09:00
@bors
Copy link
Contributor

bors commented May 20, 2023

☔ The latest upstream changes (presumably #111799) made this pull request unmergeable. Please resolve the merge conflicts.

@nikomatsakis
Copy link
Contributor

@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.

@rfcbot

This comment was marked as outdated.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. labels May 23, 2023
@nikomatsakis
Copy link
Contributor

@rfcbot fcp cancel

@rustbot labels +T-lang -T-compiler

@rfcbot
Copy link

rfcbot commented May 23, 2023

@nikomatsakis proposal cancelled.

@rustbot rustbot added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label May 23, 2023
@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label May 23, 2023
@rustbot rustbot removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label May 23, 2023
@rfcbot rfcbot removed the disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. label May 23, 2023
@nikomatsakis nikomatsakis removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label May 23, 2023
@nikomatsakis
Copy link
Contributor

@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.

@rfcbot
Copy link

rfcbot commented May 23, 2023

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.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. labels May 23, 2023
@scottmcm
Copy link
Member

scottmcm commented May 23, 2023

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 .step_by(0), which will always panic, so clippy helpfully warns about it. But the set of such things is essentially never-ending, and thus I'd much rather see them move to some general feature that libraries (including the standard library!) use to get those warnings, rather than needing to manually write out the thir-matching code for all of them. And ideally that would let r-a pick them up too!

@Urgau
Copy link
Member Author

Urgau commented May 24, 2023

While I agree with the arguments presented, I would just like to mention that the changes proposed by this lint are not stylistics!

Using env!(..) instead of option_env!(..).unwrap() makes the compiler throw an compilation error for the former and a run-time exception for the later. Which can make a huge difference in build script, having a compiler error is much nicer than a exception, the situation is even worse when a binary is installed via cargo install and expect a env at compile time as the binary may crash with some options leading to a sub-par experience for everyone.

@joshtriplett
Copy link
Member

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

@rfcbot cancel
@rfcbot merge

@rfcbot
Copy link

rfcbot commented Jun 27, 2023

@joshtriplett proposal cancelled.

@rfcbot rfcbot removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. labels Jun 27, 2023
@rfcbot
Copy link

rfcbot commented Jun 27, 2023

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.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jun 27, 2023
@RalfJung
Copy link
Member

FWIW, there are rare situations where this lint is a false positive. So maybe this shouldn't be called incorrect_X since rustc can't be sure that this is incorrect?

@Urgau
Copy link
Member Author

Urgau commented Jun 27, 2023

FWIW, there are rare situations where this lint is a false positive.

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?

@RalfJung
Copy link
Member

Yes, we used to have such code in Miri. Basically for the same reason that people use panic! instead of compile_error! one might also want option_env!(...).unwrap() instead of env!.

In the Miri case the situation was of the sort "if env var A is not set at build time then env var B must be set", which then can look like

if let Some(a) = option_env!("A") {
  // ...
} else {
  let b = option_env!("B").unwrap();
  // ...
}

Using env!("B") would clearly be wrong as then the build would fail if A is set and B is not.

@Urgau
Copy link
Member Author

Urgau commented Jun 28, 2023

Interesting example. The minimized code is indeed an issue, and the wording should be soften.
It's worth noting that the original code from Miri would not be linted against since it's using unwrap_or_else (and not unwrap/expect).

Concerning the lint name, what about suspicious_option_env_unwraps?

@RalfJung
Copy link
Member

suspicious works for me. Do we have precedent in other lints that we can follow?

@alexcrichton
Copy link
Member

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
Copy link
Member

Based on discussion in today's lang meeting, we reluctantly agreed that since this does have the potential for false positives it probably shouldn't be a rustc lint.

@rfcbot cancel
@rfcbot close

@rfcbot
Copy link

rfcbot commented Jul 11, 2023

@joshtriplett proposal cancelled.

@rfcbot rfcbot removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jul 11, 2023
@rfcbot
Copy link

rfcbot commented Jul 11, 2023

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.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Jul 11, 2023
@rfcbot
Copy link

rfcbot commented Jul 11, 2023

🔔 This is now entering its final comment period, as per the review above. 🔔

@nagisa
Copy link
Member

nagisa commented Jul 16, 2023

Using env!(..) instead of option_env!(..).unwrap() makes the compiler throw an compilation error for the former and a run-time exception for the later.

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 try_fallible_op().unwrap() or try_fallible_op().expect() rather than the panicking_op() if only to have an easier time grepping for these panic locations. Adding the two ideas together we’d lose nothing over plain env! while retaining general consistency.

(But my comment is just a more specific example of “this is a style lint” that you were responding to.)

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Jul 21, 2023
@rfcbot
Copy link

rfcbot commented Jul 21, 2023

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.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Jul 21, 2023
@Urgau Urgau closed this Jul 21, 2023
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. finished-final-comment-period The final comment period is finished for this PR / Issue. I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.