mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-25 11:57:25 +00:00
Now option_env_unwrap
warns even if a variable isn't set at compile time.
This commit is contained in:
parent
371120bdbf
commit
4e1db44404
3 changed files with 33 additions and 22 deletions
|
@ -1,10 +1,9 @@
|
||||||
use clippy_utils::diagnostics::span_lint_and_help;
|
use clippy_utils::diagnostics::span_lint_and_help;
|
||||||
use clippy_utils::is_direct_expn_of;
|
use clippy_utils::is_direct_expn_of;
|
||||||
use if_chain::if_chain;
|
|
||||||
use rustc_ast::ast::{Expr, ExprKind, MethodCall};
|
use rustc_ast::ast::{Expr, ExprKind, MethodCall};
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::sym;
|
use rustc_span::{sym, Span};
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
/// ### What it does
|
/// ### What it does
|
||||||
|
@ -36,21 +35,24 @@ declare_lint_pass!(OptionEnvUnwrap => [OPTION_ENV_UNWRAP]);
|
||||||
|
|
||||||
impl EarlyLintPass for OptionEnvUnwrap {
|
impl EarlyLintPass for OptionEnvUnwrap {
|
||||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||||
if_chain! {
|
fn lint(cx: &EarlyContext<'_>, span: Span) {
|
||||||
if let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &expr.kind;
|
span_lint_and_help(
|
||||||
if matches!(seg.ident.name, sym::expect | sym::unwrap);
|
cx,
|
||||||
if let ExprKind::Call(caller, _) = &receiver.kind;
|
OPTION_ENV_UNWRAP,
|
||||||
if is_direct_expn_of(caller.span, "option_env").is_some();
|
span,
|
||||||
then {
|
"this will panic at run-time if the environment variable doesn't exist at compile-time",
|
||||||
span_lint_and_help(
|
None,
|
||||||
cx,
|
"consider using the `env!` macro instead",
|
||||||
OPTION_ENV_UNWRAP,
|
);
|
||||||
expr.span,
|
|
||||||
"this will panic at run-time if the environment variable doesn't exist at compile-time",
|
|
||||||
None,
|
|
||||||
"consider using the `env!` macro instead"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &expr.kind &&
|
||||||
|
matches!(seg.ident.name, sym::expect | sym::unwrap) {
|
||||||
|
if let ExprKind::Call(caller, _) = &receiver.kind && is_direct_expn_of(caller.span, "option_env").is_some() {
|
||||||
|
lint(cx, expr.span);
|
||||||
|
} else if let ExprKind::Path(_, caller) = &receiver.kind && is_direct_expn_of(caller.span, "option_env").is_some() {
|
||||||
|
lint(cx, expr.span);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ use proc_macros::{external, inline_macros};
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = option_env!("PATH").unwrap();
|
let _ = option_env!("PATH").unwrap();
|
||||||
let _ = option_env!("PATH").expect("environment variable PATH isn't set");
|
let _ = option_env!("PATH").expect("environment variable PATH isn't set");
|
||||||
|
let _ = option_env!("__Y__do_not_use").unwrap(); // This test only works if you don't have a __Y__do_not_use env variable in your environment.
|
||||||
let _ = inline!(option_env!($"PATH").unwrap());
|
let _ = inline!(option_env!($"PATH").unwrap());
|
||||||
let _ = inline!(option_env!($"PATH").expect($"environment variable PATH isn't set"));
|
let _ = inline!(option_env!($"PATH").expect($"environment variable PATH isn't set"));
|
||||||
let _ = external!(option_env!($"PATH").unwrap());
|
let _ = external!(option_env!($"PATH").unwrap());
|
||||||
|
|
|
@ -16,7 +16,15 @@ LL | let _ = option_env!("PATH").expect("environment variable PATH isn't set
|
||||||
= help: consider using the `env!` macro instead
|
= help: consider using the `env!` macro instead
|
||||||
|
|
||||||
error: this will panic at run-time if the environment variable doesn't exist at compile-time
|
error: this will panic at run-time if the environment variable doesn't exist at compile-time
|
||||||
--> $DIR/option_env_unwrap.rs:12:21
|
--> $DIR/option_env_unwrap.rs:12:13
|
||||||
|
|
|
||||||
|
LL | let _ = option_env!("Y").unwrap(); // This test only works if you don't have a __Y__do_not_use env variable in your environment.
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= help: consider using the `env!` macro instead
|
||||||
|
|
||||||
|
error: this will panic at run-time if the environment variable doesn't exist at compile-time
|
||||||
|
--> $DIR/option_env_unwrap.rs:13:21
|
||||||
|
|
|
|
||||||
LL | let _ = inline!(option_env!($"PATH").unwrap());
|
LL | let _ = inline!(option_env!($"PATH").unwrap());
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
@ -25,7 +33,7 @@ LL | let _ = inline!(option_env!($"PATH").unwrap());
|
||||||
= note: this error originates in the macro `__inline_mac_fn_main` (in Nightly builds, run with -Z macro-backtrace for more info)
|
= note: this error originates in the macro `__inline_mac_fn_main` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
error: this will panic at run-time if the environment variable doesn't exist at compile-time
|
error: this will panic at run-time if the environment variable doesn't exist at compile-time
|
||||||
--> $DIR/option_env_unwrap.rs:13:21
|
--> $DIR/option_env_unwrap.rs:14:21
|
||||||
|
|
|
|
||||||
LL | let _ = inline!(option_env!($"PATH").expect($"environment variable PATH isn't set"));
|
LL | let _ = inline!(option_env!($"PATH").expect($"environment variable PATH isn't set"));
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
@ -34,7 +42,7 @@ LL | let _ = inline!(option_env!($"PATH").expect($"environment variable PATH
|
||||||
= note: this error originates in the macro `__inline_mac_fn_main` (in Nightly builds, run with -Z macro-backtrace for more info)
|
= note: this error originates in the macro `__inline_mac_fn_main` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
error: this will panic at run-time if the environment variable doesn't exist at compile-time
|
error: this will panic at run-time if the environment variable doesn't exist at compile-time
|
||||||
--> $DIR/option_env_unwrap.rs:14:13
|
--> $DIR/option_env_unwrap.rs:15:13
|
||||||
|
|
|
|
||||||
LL | let _ = external!(option_env!($"PATH").unwrap());
|
LL | let _ = external!(option_env!($"PATH").unwrap());
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
@ -43,7 +51,7 @@ LL | let _ = external!(option_env!($"PATH").unwrap());
|
||||||
= note: this error originates in the macro `external` (in Nightly builds, run with -Z macro-backtrace for more info)
|
= note: this error originates in the macro `external` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
error: this will panic at run-time if the environment variable doesn't exist at compile-time
|
error: this will panic at run-time if the environment variable doesn't exist at compile-time
|
||||||
--> $DIR/option_env_unwrap.rs:15:13
|
--> $DIR/option_env_unwrap.rs:16:13
|
||||||
|
|
|
|
||||||
LL | let _ = external!(option_env!($"PATH").expect($"environment variable PATH isn't set"));
|
LL | let _ = external!(option_env!($"PATH").expect($"environment variable PATH isn't set"));
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
@ -51,5 +59,5 @@ LL | let _ = external!(option_env!($"PATH").expect($"environment variable PA
|
||||||
= help: consider using the `env!` macro instead
|
= help: consider using the `env!` macro instead
|
||||||
= note: this error originates in the macro `external` (in Nightly builds, run with -Z macro-backtrace for more info)
|
= note: this error originates in the macro `external` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
error: aborting due to 6 previous errors
|
error: aborting due to 7 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue