1
1
use rustc:: hir:: * ;
2
- use rustc:: hir:: map:: Node :: NodeItem ;
3
2
use rustc:: lint:: * ;
4
3
use rustc:: ty;
5
4
use syntax:: ast:: LitKind ;
6
- use syntax:: symbol:: InternedString ;
7
5
use utils:: paths;
8
6
use utils:: { is_expn_of, match_def_path, match_type, resolve_node, span_lint, walk_ptrs_ty, opt_def_id} ;
9
7
@@ -50,8 +48,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
50
48
let Some ( fun_def_id) = opt_def_id( resolve_node( cx, qpath, fun. hir_id) ) ,
51
49
match_def_path( cx. tcx, fun_def_id, & paths:: FMT_ARGUMENTS_NEWV1 ) ,
52
50
// ensure the format string is `"{..}"` with only one argument and no text
53
- check_static_str( cx , & args[ 0 ] ) ,
51
+ check_static_str( & args[ 0 ] ) ,
54
52
// ensure the format argument is `{}` ie. Display with no fancy option
53
+ // and that the argument is a string
55
54
check_arg_is_display( cx, & args[ 1 ] )
56
55
] , {
57
56
span_lint( cx, USELESS_FORMAT , span, "useless use of `format!`" ) ;
@@ -69,44 +68,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
69
68
}
70
69
}
71
70
72
- /// Returns the slice of format string parts in an `Arguments::new_v1` call.
73
- /// Public because it's shared with a lint in print.rs.
74
- pub fn get_argument_fmtstr_parts < ' a , ' b > ( cx : & LateContext < ' a , ' b > , expr : & ' a Expr ) -> Option < Vec < InternedString > > {
71
+ /// Checks if the expressions matches `&[""]`
72
+ fn check_static_str ( expr : & Expr ) -> bool {
75
73
if_let_chain ! { [
76
- let ExprBlock ( ref block) = expr. node,
77
- block. stmts. len( ) == 1 ,
78
- let StmtDecl ( ref decl, _) = block. stmts[ 0 ] . node,
79
- let DeclItem ( ref decl) = decl. node,
80
- let Some ( NodeItem ( decl) ) = cx. tcx. hir. find( decl. id) ,
81
- decl. name == "__STATIC_FMTSTR" ,
82
- let ItemStatic ( _, _, ref expr) = decl. node,
83
- let ExprAddrOf ( _, ref expr) = cx. tcx. hir. body( * expr) . value. node, // &["…", "…", …]
84
- let ExprArray ( ref exprs) = expr. node,
74
+ let ExprAddrOf ( _, ref expr) = expr. node, // &[""]
75
+ let ExprArray ( ref exprs) = expr. node, // [""]
76
+ exprs. len( ) == 1 ,
77
+ let ExprLit ( ref lit) = exprs[ 0 ] . node,
78
+ let LitKind :: Str ( ref lit, _) = lit. node,
85
79
] , {
86
- let mut result = Vec :: new( ) ;
87
- for expr in exprs {
88
- if let ExprLit ( ref lit) = expr. node {
89
- if let LitKind :: Str ( ref lit, _) = lit. node {
90
- result. push( lit. as_str( ) ) ;
91
- }
92
- }
93
- }
94
- return Some ( result) ;
80
+ return lit. as_str( ) . is_empty( ) ;
95
81
} }
96
- None
97
- }
98
82
99
- /// Checks if the expressions matches
100
- /// ```rust, ignore
101
- /// { static __STATIC_FMTSTR: &'static[&'static str] = &["a", "b", c];
102
- /// __STATIC_FMTSTR }
103
- /// ```
104
- fn check_static_str ( cx : & LateContext , expr : & Expr ) -> bool {
105
- if let Some ( expr) = get_argument_fmtstr_parts ( cx, expr) {
106
- expr. len ( ) == 1 && expr[ 0 ] . is_empty ( )
107
- } else {
108
- false
109
- }
83
+ false
110
84
}
111
85
112
86
/// Checks if the expressions matches
0 commit comments