11use crate :: consts:: { constant, Constant } ;
22use crate :: utils:: paths;
3- use crate :: utils:: { is_direct_expn_of, is_expn_of, match_def_path, resolve_node , span_help_and_lint } ;
3+ use crate :: utils:: { is_direct_expn_of, is_expn_of, match_def_path, span_help_and_lint , snippet } ;
44use if_chain:: if_chain;
55use rustc:: hir:: * ;
66use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
77use rustc:: { declare_lint_pass, declare_tool_lint} ;
88use syntax:: ast:: LitKind ;
9- use syntax :: source_map :: symbol :: LocalInternedString ;
9+ use std :: borrow :: Cow ;
1010
1111declare_clippy_lint ! {
1212 /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
@@ -75,7 +75,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
7575 "`assert!(true)` will be optimized out by the compiler" ,
7676 "remove it" ,
7777 ) ;
78- } else if panic_message. starts_with ( "assertion failed: " ) {
78+ } else if panic_message. is_empty ( ) || panic_message . starts_with ( " \ " assertion failed: ") {
7979 span_help_and_lint (
8080 cx,
8181 ASSERTIONS_ON_CONSTANTS ,
@@ -88,9 +88,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
8888 cx,
8989 ASSERTIONS_ON_CONSTANTS ,
9090 e. span ,
91- & format ! ( "`assert!(false, \" {} \" )` should probably be replaced" , panic_message, ) ,
91+ & format ! ( "`assert!(false, {} )` should probably be replaced" , panic_message, ) ,
9292 & format ! (
93- "use `panic!(\" {} \" )` or `unreachable!(\" {} \" )`" ,
93+ "use `panic!({} )` or `unreachable!({} )`" ,
9494 panic_message, panic_message,
9595 ) ,
9696 ) ;
@@ -119,7 +119,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
119119///
120120/// Returns the `message` argument of `begin_panic` and the value of `c` which is the
121121/// first argument of `assert!`.
122- fn assert_with_message < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr ) -> Option < ( LocalInternedString , bool ) > {
122+ fn assert_with_message < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr ) -> Option < ( Cow < ' a , str > , bool ) > {
123123 if_chain ! {
124124 if let ExprKind :: Match ( ref expr, ref arms, _) = expr. kind;
125125 // matches { let _t = expr; _t }
@@ -140,14 +140,12 @@ fn assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -
140140 // function call
141141 if let Some ( args) = match_function_call( cx, begin_panic_call, & paths:: BEGIN_PANIC ) ;
142142 if args. len( ) == 2 ;
143- if let ExprKind :: Lit ( ref lit) = args[ 0 ] . kind;
144- if let LitKind :: Str ( ref s, _) = lit. node;
145143 // bind the second argument of the `assert!` macro
146- let panic_message = s . as_str ( ) ;
144+ let panic_message_arg = snippet ( cx , args [ 0 ] . span , ".." ) ;
147145 // second argument of begin_panic is irrelevant
148146 // as is the second match arm
149147 then {
150- return Some ( ( panic_message , is_true) ) ;
148+ return Some ( ( panic_message_arg , is_true) ) ;
151149 }
152150 }
153151 None
@@ -164,7 +162,7 @@ fn match_function_call<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, p
164162 if_chain ! {
165163 if let ExprKind :: Call ( ref fun, ref args) = expr. kind;
166164 if let ExprKind :: Path ( ref qpath) = fun. kind;
167- if let Some ( fun_def_id) = resolve_node ( cx , qpath, fun. hir_id) . opt_def_id( ) ;
165+ if let Some ( fun_def_id) = cx . tables . qpath_res ( qpath, fun. hir_id) . opt_def_id( ) ;
168166 if match_def_path( cx, fun_def_id, path) ;
169167 then {
170168 return Some ( & args)
0 commit comments