1
1
use crate :: consts:: { constant, Constant } ;
2
2
use 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 } ;
4
4
use if_chain:: if_chain;
5
5
use rustc:: hir:: * ;
6
6
use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
7
7
use rustc:: { declare_lint_pass, declare_tool_lint} ;
8
8
use syntax:: ast:: LitKind ;
9
- use syntax :: source_map :: symbol :: LocalInternedString ;
9
+ use std :: borrow :: Cow ;
10
10
11
11
declare_clippy_lint ! {
12
12
/// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
@@ -75,7 +75,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
75
75
"`assert!(true)` will be optimized out by the compiler" ,
76
76
"remove it" ,
77
77
) ;
78
- } else if panic_message. starts_with ( "assertion failed: " ) {
78
+ } else if panic_message. is_empty ( ) || panic_message . starts_with ( " \ " assertion failed: ") {
79
79
span_help_and_lint (
80
80
cx,
81
81
ASSERTIONS_ON_CONSTANTS ,
@@ -88,9 +88,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
88
88
cx,
89
89
ASSERTIONS_ON_CONSTANTS ,
90
90
e. span ,
91
- & format ! ( "`assert!(false, \" {} \" )` should probably be replaced" , panic_message, ) ,
91
+ & format ! ( "`assert!(false, {} )` should probably be replaced" , panic_message, ) ,
92
92
& format ! (
93
- "use `panic!(\" {} \" )` or `unreachable!(\" {} \" )`" ,
93
+ "use `panic!({} )` or `unreachable!({} )`" ,
94
94
panic_message, panic_message,
95
95
) ,
96
96
) ;
@@ -119,7 +119,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
119
119
///
120
120
/// Returns the `message` argument of `begin_panic` and the value of `c` which is the
121
121
/// 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 ) > {
123
123
if_chain ! {
124
124
if let ExprKind :: Match ( ref expr, ref arms, _) = expr. kind;
125
125
// matches { let _t = expr; _t }
@@ -140,14 +140,12 @@ fn assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -
140
140
// function call
141
141
if let Some ( args) = match_function_call( cx, begin_panic_call, & paths:: BEGIN_PANIC ) ;
142
142
if args. len( ) == 2 ;
143
- if let ExprKind :: Lit ( ref lit) = args[ 0 ] . kind;
144
- if let LitKind :: Str ( ref s, _) = lit. node;
145
143
// 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 , ".." ) ;
147
145
// second argument of begin_panic is irrelevant
148
146
// as is the second match arm
149
147
then {
150
- return Some ( ( panic_message , is_true) ) ;
148
+ return Some ( ( panic_message_arg , is_true) ) ;
151
149
}
152
150
}
153
151
None
@@ -164,7 +162,7 @@ fn match_function_call<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, p
164
162
if_chain ! {
165
163
if let ExprKind :: Call ( ref fun, ref args) = expr. kind;
166
164
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( ) ;
168
166
if match_def_path( cx, fun_def_id, path) ;
169
167
then {
170
168
return Some ( & args)
0 commit comments