From 72a5d7b612b5e6ef1dbf0cff88dbfded744c280a Mon Sep 17 00:00:00 2001 From: Matthias Seiffert Date: Sat, 5 Oct 2019 16:45:02 +0200 Subject: [PATCH 1/4] Add message to replace assert!(false) help --- clippy_lints/src/assertions_on_constants.rs | 98 +++++++++++++++++++-- tests/ui/assertions_on_constants.rs | 1 + tests/ui/assertions_on_constants.stderr | 16 +++- 3 files changed, 106 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index d1be15ba166..79b584990f8 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -1,9 +1,11 @@ -use rustc::hir::{Expr, ExprKind}; +use crate::consts::{constant, Constant}; +use crate::utils::{is_direct_expn_of, is_expn_of, match_qpath, span_help_and_lint}; +use if_chain::if_chain; +use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; - -use crate::consts::{constant, Constant}; -use crate::utils::{is_direct_expn_of, is_expn_of, span_help_and_lint}; +use syntax::ast::LitKind; +use syntax::source_map::symbol::LocalInternedString; declare_clippy_lint! { /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls. @@ -63,7 +65,93 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { if assert_span.from_expansion() { return; } - lint_assert_cb(false); + if let Some((panic_message, is_true)) = assert_with_message(&cx, e) { + if is_true { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(true)` will be optimized out by the compiler", + "remove it", + ); + } else if panic_message.starts_with("assertion failed: ") { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(false)` should probably be replaced", + "use `panic!()` or `unreachable!()`", + ); + } else { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + &format!("`assert!(false, \"{}\")` should probably be replaced", panic_message,), + &format!( + "use `panic!(\"{}\")` or `unreachable!(\"{}\")`", + panic_message, panic_message, + ), + ); + } + } } } } + +// fn get_assert_args(snip: String) -> Option> { +// +// } + +fn assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<(LocalInternedString, bool)> { + if_chain! { + if let ExprKind::Match(ref expr, ref arms, MatchSource::IfDesugar { contains_else_clause: false }) = expr.kind; + // match expr + if let ExprKind::DropTemps(ref expr) = expr.kind; + if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind; + //if let ExprKind::Lit(ref lit) = expr.kind; + if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, expr); + //if is_true; + // match arms + // arm 1 pattern + if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind; + if let ExprKind::Lit(ref lit) = lit_expr.kind; + if let LitKind::Bool(true) = lit.node; + //if let LitKind::Bool(true) = lit1.node; + // arm 1 block + if let ExprKind::Block(ref block1, _) = arms[0].body.kind; + if let Some(trailing_expr1) = &block1.expr; + if block1.stmts.len() == 0; + // + if let ExprKind::Block(ref actual_block1, _) = trailing_expr1.kind; + if let Some(block1_expr) = &actual_block1.expr; + // function call + if let ExprKind::Call(ref func, ref args) = block1_expr.kind; + if let ExprKind::Path(ref path) = func.kind; + // ["{{root}}", "std", "rt", "begin_panic"] does not work + if match_qpath(path, &["$crate", "rt", "begin_panic"]); + // arguments + if args.len() == 2; + if let ExprKind::Lit(ref lit) = args[0].kind; + if let LitKind::Str(ref s, _) = lit.node; + let panic_message = s.as_str(); // bind the panic message + if let ExprKind::AddrOf(MutImmutable, ref inner) = args[1].kind; + if let ExprKind::Tup(ref elements) = inner.kind; + if elements.len() == 3; + if let ExprKind::Lit(ref lit1) = elements[0].kind; + if let LitKind::Str(ref s1, _) = lit1.node; + if let ExprKind::Lit(ref lit2) = elements[1].kind; + if let LitKind::Int(_, _) = lit2.node; + if let ExprKind::Lit(ref lit3) = elements[2].kind; + if let LitKind::Int(_, _) = lit3.node; + // arm 2 block + if let PatKind::Wild = arms[1].pat.kind; + if let ExprKind::Block(ref block2, _) = arms[1].body.kind; + if let None = &block2.expr; + if block2.stmts.len() == 0; + then { + return Some((panic_message, is_true)); + } + } + return None; +} diff --git a/tests/ui/assertions_on_constants.rs b/tests/ui/assertions_on_constants.rs index aee6cc505ab..0d9fe636bf9 100644 --- a/tests/ui/assertions_on_constants.rs +++ b/tests/ui/assertions_on_constants.rs @@ -16,6 +16,7 @@ fn main() { const C: bool = false; assert!(C); + assert!(C, "C message"); debug_assert!(true); // Don't lint this, since there is no better way for expressing "Only panic in debug mode". diff --git a/tests/ui/assertions_on_constants.stderr b/tests/ui/assertions_on_constants.stderr index c22da856975..84a2533ccdc 100644 --- a/tests/ui/assertions_on_constants.stderr +++ b/tests/ui/assertions_on_constants.stderr @@ -23,13 +23,13 @@ LL | assert!(true, "true message"); | = help: remove it -error: `assert!(false)` should probably be replaced +error: `assert!(false, "false message")` should probably be replaced --> $DIR/assertions_on_constants.rs:12:5 | LL | assert!(false, "false message"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = help: use `panic!()` or `unreachable!()` + = help: use `panic!("false message")` or `unreachable!("false message")` error: `assert!(true)` will be optimized out by the compiler --> $DIR/assertions_on_constants.rs:15:5 @@ -47,8 +47,16 @@ LL | assert!(C); | = help: use `panic!()` or `unreachable!()` +error: `assert!(false, "C message")` should probably be replaced + --> $DIR/assertions_on_constants.rs:19:5 + | +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:20:5 + --> $DIR/assertions_on_constants.rs:21:5 | LL | debug_assert!(true); | ^^^^^^^^^^^^^^^^^^^^ @@ -56,5 +64,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 7 previous errors +error: aborting due to 8 previous errors From d66acc23a92a966c711382ac8b86c006fddb4fd2 Mon Sep 17 00:00:00 2001 From: Matthias Seiffert Date: Sun, 6 Oct 2019 20:10:30 +0200 Subject: [PATCH 2/4] Make if_chain more readable --- clippy_lints/src/assertions_on_constants.rs | 93 ++++++++++++--------- 1 file changed, 55 insertions(+), 38 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index 79b584990f8..aff52de0fac 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -1,5 +1,6 @@ use crate::consts::{constant, Constant}; -use crate::utils::{is_direct_expn_of, is_expn_of, match_qpath, span_help_and_lint}; +use crate::utils::paths; +use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, resolve_node, span_help_and_lint}; use if_chain::if_chain; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; @@ -99,59 +100,75 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { } } -// fn get_assert_args(snip: String) -> Option> { -// -// } - +/// Check if the expression matches +/// +/// ```rust,ignore +/// match { let _t = !c; _t } { +/// true => { +/// { +/// ::std::rt::begin_panic(message, _) +/// } +/// } +/// _ => { } +/// }; +/// ``` +/// +/// where `message` is a string literal and `c` is a constant bool. +/// +/// TODO extend this to match anything as message not just string literals +/// +/// 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)> { if_chain! { - if let ExprKind::Match(ref expr, ref arms, MatchSource::IfDesugar { contains_else_clause: false }) = expr.kind; - // match expr + if let ExprKind::Match(ref expr, ref arms, _) = expr.kind; + // matches { let _t = expr; _t } if let ExprKind::DropTemps(ref expr) = expr.kind; if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind; - //if let ExprKind::Lit(ref lit) = expr.kind; + // bind the first argument of the `assert!` macro if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, expr); - //if is_true; - // match arms // arm 1 pattern if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind; if let ExprKind::Lit(ref lit) = lit_expr.kind; if let LitKind::Bool(true) = lit.node; - //if let LitKind::Bool(true) = lit1.node; // arm 1 block - if let ExprKind::Block(ref block1, _) = arms[0].body.kind; - if let Some(trailing_expr1) = &block1.expr; - if block1.stmts.len() == 0; - // - if let ExprKind::Block(ref actual_block1, _) = trailing_expr1.kind; - if let Some(block1_expr) = &actual_block1.expr; + if let ExprKind::Block(ref block, _) = arms[0].body.kind; + if block.stmts.len() == 0; + if let Some(block_expr) = &block.expr; + if let ExprKind::Block(ref inner_block, _) = block_expr.kind; + if let Some(begin_panic_call) = &inner_block.expr; // function call - if let ExprKind::Call(ref func, ref args) = block1_expr.kind; - if let ExprKind::Path(ref path) = func.kind; - // ["{{root}}", "std", "rt", "begin_panic"] does not work - if match_qpath(path, &["$crate", "rt", "begin_panic"]); - // arguments + 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; - let panic_message = s.as_str(); // bind the panic message - if let ExprKind::AddrOf(MutImmutable, ref inner) = args[1].kind; - if let ExprKind::Tup(ref elements) = inner.kind; - if elements.len() == 3; - if let ExprKind::Lit(ref lit1) = elements[0].kind; - if let LitKind::Str(ref s1, _) = lit1.node; - if let ExprKind::Lit(ref lit2) = elements[1].kind; - if let LitKind::Int(_, _) = lit2.node; - if let ExprKind::Lit(ref lit3) = elements[2].kind; - if let LitKind::Int(_, _) = lit3.node; - // arm 2 block - if let PatKind::Wild = arms[1].pat.kind; - if let ExprKind::Block(ref block2, _) = arms[1].body.kind; - if let None = &block2.expr; - if block2.stmts.len() == 0; + // bind the second argument of the `assert!` macro + let panic_message = s.as_str(); + // second argument of begin_panic is irrelevant + // as is the second match arm then { return Some((panic_message, is_true)); } } - return None; + None +} + +/// Matches a function call with the given path and returns the arguments. +/// +/// Usage: +/// +/// ```rust,ignore +/// if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC); +/// ``` +fn match_function_call<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, path: &[&str]) -> Option<&'a [Expr]> { + 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 match_def_path(cx, fun_def_id, path); + then { + return Some(&args) + } + }; + None } From 22f057972f6b8ae3b6df47c70ba1d610c13f4218 Mon Sep 17 00:00:00 2001 From: Matthias Seiffert Date: Mon, 7 Oct 2019 20:40:05 +0200 Subject: [PATCH 3/4] Match any expr for panic message --- clippy_lints/src/assertions_on_constants.rs | 20 +++++++++----------- tests/ui/assertions_on_constants.rs | 3 +++ 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 From 6ee8d751f63ab3652297aa88adafc6ca549cb899 Mon Sep 17 00:00:00 2001 From: Matthias Seiffert Date: Mon, 7 Oct 2019 22:08:00 +0200 Subject: [PATCH 4/4] Reduce duplication --- clippy_lints/src/assertions_on_constants.rs | 123 ++++++++++---------- 1 file changed, 59 insertions(+), 64 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index 5bb57be0013..3f84f322500 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -1,12 +1,11 @@ use crate::consts::{constant, Constant}; use crate::utils::paths; -use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, span_help_and_lint, snippet}; +use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, snippet_opt, span_help_and_lint}; 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 std::borrow::Cow; declare_clippy_lint! { /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls. @@ -34,72 +33,68 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { - let lint_assert_cb = |is_debug_assert: bool| { - if let ExprKind::Unary(_, ref lit) = e.kind { - if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit) { - if is_true { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(true)` will be optimized out by the compiler", - "remove it", - ); - } else if !is_debug_assert { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(false)` should probably be replaced", - "use `panic!()` or `unreachable!()`", - ); - } - } - } + let lint_true = || { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(true)` will be optimized out by the compiler", + "remove it", + ); }; + let lint_false_without_message = || { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(false)` should probably be replaced", + "use `panic!()` or `unreachable!()`", + ); + }; + let lint_false_with_message = |panic_message: String| { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + &format!("`assert!(false, {})` should probably be replaced", panic_message), + &format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message), + ) + }; + if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") { if debug_assert_span.from_expansion() { return; } - lint_assert_cb(true); + if_chain! { + if let ExprKind::Unary(_, ref lit) = e.kind; + if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit); + if is_true; + then { + lint_true(); + } + }; } else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") { if assert_span.from_expansion() { return; } - if let Some((panic_message, is_true)) = assert_with_message(&cx, e) { - if is_true { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(true)` will be optimized out by the compiler", - "remove it", - ); - } else if panic_message.is_empty() || panic_message.starts_with("\"assertion failed: ") { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(false)` should probably be replaced", - "use `panic!()` or `unreachable!()`", - ); - } else { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - &format!("`assert!(false, {})` should probably be replaced", panic_message,), - &format!( - "use `panic!({})` or `unreachable!({})`", - panic_message, panic_message, - ), - ); - } + if let Some(assert_match) = match_assert_with_message(&cx, e) { + match assert_match { + // matched assert but not message + AssertKind::WithoutMessage(false) => lint_false_without_message(), + AssertKind::WithoutMessage(true) | AssertKind::WithMessage(_, true) => lint_true(), + AssertKind::WithMessage(panic_message, false) => lint_false_with_message(panic_message), + }; } } } } +/// Result of calling `match_assert_with_message`. +enum AssertKind { + WithMessage(String, bool), + WithoutMessage(bool), +} + /// Check if the expression matches /// /// ```rust,ignore @@ -113,13 +108,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { /// }; /// ``` /// -/// where `message` is a string literal and `c` is a constant bool. -/// -/// TODO extend this to match anything as message not just string literals -/// -/// 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<(Cow<'a, str>, bool)> { +/// where `message` is any expression and `c` is a constant bool. +fn match_assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option { if_chain! { if let ExprKind::Match(ref expr, ref arms, _) = expr.kind; // matches { let _t = expr; _t } @@ -140,12 +130,17 @@ 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; - // bind the second argument of the `assert!` macro - let panic_message_arg = snippet(cx, args[0].span, ".."); + // bind the second argument of the `assert!` macro if it exists + if let panic_message = snippet_opt(cx, args[0].span); // second argument of begin_panic is irrelevant // as is the second match arm then { - return Some((panic_message_arg, is_true)); + // an empty message occurs when it was generated by the macro + // (and not passed by the user) + return panic_message + .filter(|msg| !msg.is_empty()) + .map(|msg| AssertKind::WithMessage(msg, is_true)) + .or(Some(AssertKind::WithoutMessage(is_true))); } } None