1
1
use if_chain:: if_chain;
2
2
use rustc_errors:: Applicability ;
3
- use rustc_hir:: * ;
3
+ use rustc_hir:: { def , Expr , ExprKind , PrimTy , QPath , TyKind } ;
4
4
use rustc_lint:: { LateContext , LateLintPass } ;
5
+ use rustc_middle:: ty:: Ty ;
5
6
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
7
+ use rustc_span:: symbol:: sym;
6
8
9
+ use crate :: utils:: is_type_diagnostic_item;
7
10
use crate :: utils:: span_lint_and_sugg;
8
11
use crate :: utils:: sugg:: Sugg ;
9
12
@@ -40,8 +43,7 @@ impl LateLintPass<'tcx> for FromStrRadix10 {
40
43
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , exp : & Expr < ' tcx > ) {
41
44
if_chain ! {
42
45
if let ExprKind :: Call ( maybe_path, arguments) = & exp. kind;
43
- if let ExprKind :: Path ( qpath) = & maybe_path. kind;
44
- if let QPath :: TypeRelative ( ty, pathseg) = & qpath;
46
+ if let ExprKind :: Path ( QPath :: TypeRelative ( ty, pathseg) ) = & maybe_path. kind;
45
47
46
48
// check if the first part of the path is some integer primitive
47
49
if let TyKind :: Path ( ty_qpath) = & ty. kind;
@@ -59,9 +61,20 @@ impl LateLintPass<'tcx> for FromStrRadix10 {
59
61
if let rustc_ast:: ast:: LitKind :: Int ( 10 , _) = lit. node;
60
62
61
63
then {
64
+ let expr = if let ExprKind :: AddrOf ( _, _, expr) = & arguments[ 0 ] . kind {
65
+ let ty = cx. typeck_results( ) . expr_ty( expr) ;
66
+ if is_ty_stringish( cx, ty) {
67
+ expr
68
+ } else {
69
+ & arguments[ 0 ]
70
+ }
71
+ } else {
72
+ & arguments[ 0 ]
73
+ } ;
74
+
62
75
let sugg = Sugg :: hir_with_applicability(
63
76
cx,
64
- & arguments [ 0 ] ,
77
+ expr ,
65
78
"<string>" ,
66
79
& mut Applicability :: MachineApplicable
67
80
) . maybe_par( ) ;
@@ -79,3 +92,8 @@ impl LateLintPass<'tcx> for FromStrRadix10 {
79
92
}
80
93
}
81
94
}
95
+
96
+ /// Checks if a Ty is `String` or `&str`
97
+ fn is_ty_stringish ( cx : & LateContext < ' _ > , ty : Ty < ' _ > ) -> bool {
98
+ is_type_diagnostic_item ( cx, ty, sym:: string_type) || is_type_diagnostic_item ( cx, ty, sym:: str)
99
+ }
0 commit comments