diff options
| author | Matthias Seiffert <matthias-seiffert@hotmail.de> | 2019-10-07 20:40:05 +0200 |
|---|---|---|
| committer | Matthias Seiffert <matthias-seiffert@hotmail.de> | 2019-10-07 20:40:05 +0200 |
| commit | 22f057972f6b8ae3b6df47c70ba1d610c13f4218 (patch) | |
| tree | dda39d91381f75cb37c8b6f079364db0a4982d34 | |
| parent | d66acc23a92a966c711382ac8b86c006fddb4fd2 (diff) | |
| download | rust-22f057972f6b8ae3b6df47c70ba1d610c13f4218.tar.gz rust-22f057972f6b8ae3b6df47c70ba1d610c13f4218.zip | |
Match any expr for panic message
| -rw-r--r-- | clippy_lints/src/assertions_on_constants.rs | 20 | ||||
| -rw-r--r-- | tests/ui/assertions_on_constants.rs | 3 | ||||
| -rw-r--r-- | tests/ui/assertions_on_constants.stderr | 18 |
3 files changed, 25 insertions, 16 deletions
diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index aff52de0fac..5bb57be0013 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -1,12 +1,12 @@ use crate::consts::{constant, Constant}; use crate::utils::paths; -use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, resolve_node, span_help_and_lint}; +use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, span_help_and_lint, snippet}; use if_chain::if_chain; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; use syntax::ast::LitKind; -use syntax::source_map::symbol::LocalInternedString; +use std::borrow::Cow; declare_clippy_lint! { /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls. @@ -75,7 +75,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { "`assert!(true)` will be optimized out by the compiler", "remove it", ); - } else if panic_message.starts_with("assertion failed: ") { + } else if panic_message.is_empty() || panic_message.starts_with("\"assertion failed: ") { span_help_and_lint( cx, ASSERTIONS_ON_CONSTANTS, @@ -88,9 +88,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { cx, ASSERTIONS_ON_CONSTANTS, e.span, - &format!("`assert!(false, \"{}\")` should probably be replaced", panic_message,), + &format!("`assert!(false, {})` should probably be replaced", panic_message,), &format!( - "use `panic!(\"{}\")` or `unreachable!(\"{}\")`", + "use `panic!({})` or `unreachable!({})`", panic_message, panic_message, ), ); @@ -119,7 +119,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { /// /// Returns the `message` argument of `begin_panic` and the value of `c` which is the /// first argument of `assert!`. -fn assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<(LocalInternedString, bool)> { +fn assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<(Cow<'a, str>, bool)> { if_chain! { if let ExprKind::Match(ref expr, ref arms, _) = expr.kind; // matches { let _t = expr; _t } @@ -140,14 +140,12 @@ fn assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) - // function call if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC); if args.len() == 2; - if let ExprKind::Lit(ref lit) = args[0].kind; - if let LitKind::Str(ref s, _) = lit.node; // bind the second argument of the `assert!` macro - let panic_message = s.as_str(); + let panic_message_arg = snippet(cx, args[0].span, ".."); // second argument of begin_panic is irrelevant // as is the second match arm then { - return Some((panic_message, is_true)); + return Some((panic_message_arg, is_true)); } } None @@ -164,7 +162,7 @@ fn match_function_call<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, p if_chain! { if let ExprKind::Call(ref fun, ref args) = expr.kind; if let ExprKind::Path(ref qpath) = fun.kind; - if let Some(fun_def_id) = resolve_node(cx, qpath, fun.hir_id).opt_def_id(); + if let Some(fun_def_id) = cx.tables.qpath_res(qpath, fun.hir_id).opt_def_id(); if match_def_path(cx, fun_def_id, path); then { return Some(&args) diff --git a/tests/ui/assertions_on_constants.rs b/tests/ui/assertions_on_constants.rs index 0d9fe636bf9..60d721c2f20 100644 --- a/tests/ui/assertions_on_constants.rs +++ b/tests/ui/assertions_on_constants.rs @@ -11,6 +11,9 @@ fn main() { assert!(true, "true message"); assert!(false, "false message"); + let msg = "panic message"; + assert!(false, msg.to_uppercase()); + const B: bool = true; assert!(B); diff --git a/tests/ui/assertions_on_constants.stderr b/tests/ui/assertions_on_constants.stderr index 84a2533ccdc..215012f2ad7 100644 --- a/tests/ui/assertions_on_constants.stderr +++ b/tests/ui/assertions_on_constants.stderr @@ -31,16 +31,24 @@ LL | assert!(false, "false message"); | = help: use `panic!("false message")` or `unreachable!("false message")` -error: `assert!(true)` will be optimized out by the compiler +error: `assert!(false, msg.to_uppercase())` should probably be replaced --> $DIR/assertions_on_constants.rs:15:5 | +LL | assert!(false, msg.to_uppercase()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: use `panic!(msg.to_uppercase())` or `unreachable!(msg.to_uppercase())` + +error: `assert!(true)` will be optimized out by the compiler + --> $DIR/assertions_on_constants.rs:18:5 + | LL | assert!(B); | ^^^^^^^^^^^ | = help: remove it error: `assert!(false)` should probably be replaced - --> $DIR/assertions_on_constants.rs:18:5 + --> $DIR/assertions_on_constants.rs:21:5 | LL | assert!(C); | ^^^^^^^^^^^ @@ -48,7 +56,7 @@ LL | assert!(C); = help: use `panic!()` or `unreachable!()` error: `assert!(false, "C message")` should probably be replaced - --> $DIR/assertions_on_constants.rs:19:5 + --> $DIR/assertions_on_constants.rs:22:5 | LL | assert!(C, "C message"); | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -56,7 +64,7 @@ LL | assert!(C, "C message"); = help: use `panic!("C message")` or `unreachable!("C message")` error: `assert!(true)` will be optimized out by the compiler - --> $DIR/assertions_on_constants.rs:21:5 + --> $DIR/assertions_on_constants.rs:24:5 | LL | debug_assert!(true); | ^^^^^^^^^^^^^^^^^^^^ @@ -64,5 +72,5 @@ LL | debug_assert!(true); = help: remove it = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) -error: aborting due to 8 previous errors +error: aborting due to 9 previous errors |
