@@ -5,15 +5,26 @@ use clippy_utils::source::snippet;
55use clippy_utils:: ty:: is_type_diagnostic_item;
66use clippy_utils:: visitors:: for_each_local_use_after_expr;
77use clippy_utils:: { get_parent_expr, match_def_path} ;
8+ use rustc_ast:: LitKind ;
89use rustc_errors:: Applicability ;
910use rustc_hir:: def:: Res ;
10- use rustc_hir:: { Expr , ExprKind , QPath } ;
11+ use rustc_hir:: { BinOpKind , Expr , ExprKind , QPath } ;
1112use rustc_lint:: LateContext ;
1213use rustc_middle:: ty:: { self , Ty } ;
1314use rustc_span:: sym;
1415
1516use super :: READ_LINE_WITHOUT_TRIM ;
1617
18+ fn expr_is_string_literal_without_trailing_newline ( expr : & Expr < ' _ > ) -> bool {
19+ if let ExprKind :: Lit ( lit) = expr. kind
20+ && let LitKind :: Str ( sym, _) = lit. node
21+ {
22+ !sym. as_str ( ) . ends_with ( '\n' )
23+ } else {
24+ false
25+ }
26+ }
27+
1728/// Will a `.parse::<ty>()` call fail if the input has a trailing newline?
1829fn parse_fails_on_trailing_newline ( ty : Ty < ' _ > ) -> bool {
1930 // only allow a very limited set of types for now, for which we 100% know parsing will fail
@@ -27,30 +38,66 @@ pub fn check(cx: &LateContext<'_>, call: &Expr<'_>, recv: &Expr<'_>, arg: &Expr<
2738 && let Res :: Local ( local_id) = path. res
2839 {
2940 // We've checked that `call` is a call to `Stdin::read_line()` with the right receiver,
30- // now let's check if the first use of the string passed to `::read_line()` is
31- // parsed into a type that will always fail if it has a trailing newline.
41+ // now let's check if the first use of the string passed to `::read_line()`
42+ // is used for operations that will always fail (e.g. parsing "6\n" into a number)
3243 for_each_local_use_after_expr ( cx, local_id, call. hir_id , |expr| {
33- if let Some ( parent) = get_parent_expr ( cx, expr)
34- && let ExprKind :: MethodCall ( segment, .., span) = parent. kind
35- && segment. ident . name == sym ! ( parse)
36- && let parse_result_ty = cx. typeck_results ( ) . expr_ty ( parent)
37- && is_type_diagnostic_item ( cx, parse_result_ty, sym:: Result )
38- && let ty:: Adt ( _, args) = parse_result_ty. kind ( )
39- && let Some ( ok_ty) = args[ 0 ] . as_type ( )
40- && parse_fails_on_trailing_newline ( ok_ty)
41- {
42- let local_snippet = snippet ( cx, expr. span , "<expr>" ) ;
43- span_lint_and_then (
44- cx,
45- READ_LINE_WITHOUT_TRIM ,
46- span,
47- "calling `.parse()` without trimming the trailing newline character" ,
48- |diag| {
44+ if let Some ( parent) = get_parent_expr ( cx, expr) {
45+ let data = if let ExprKind :: MethodCall ( segment, recv, args, span) = parent. kind {
46+ if segment. ident . name == sym ! ( parse)
47+ && let parse_result_ty = cx. typeck_results ( ) . expr_ty ( parent)
48+ && is_type_diagnostic_item ( cx, parse_result_ty, sym:: Result )
49+ && let ty:: Adt ( _, substs) = parse_result_ty. kind ( )
50+ && let Some ( ok_ty) = substs[ 0 ] . as_type ( )
51+ && parse_fails_on_trailing_newline ( ok_ty)
52+ {
53+ // Called `s.parse::<T>()` where `T` is a type we know for certain will fail
54+ // if the input has a trailing newline
55+ Some ( (
56+ span,
57+ "calling `.parse()` on a string without trimming the trailing newline character" ,
58+ "checking" ,
59+ ) )
60+ } else if segment. ident . name == sym ! ( ends_with)
61+ && recv. span == expr. span
62+ && let [ arg] = args
63+ && expr_is_string_literal_without_trailing_newline ( arg)
64+ {
65+ // Called `s.ends_with(<some string literal>)` where the argument is a string literal that does
66+ // not end with a newline, thus always evaluating to false
67+ Some ( (
68+ parent. span ,
69+ "checking the end of a string without trimming the trailing newline character" ,
70+ "parsing" ,
71+ ) )
72+ } else {
73+ None
74+ }
75+ } else if let ExprKind :: Binary ( binop, left, right) = parent. kind
76+ && let BinOpKind :: Eq = binop. node
77+ && ( expr_is_string_literal_without_trailing_newline ( left)
78+ || expr_is_string_literal_without_trailing_newline ( right) )
79+ {
80+ // `s == <some string literal>` where the string literal does not end with a newline
81+ Some ( (
82+ parent. span ,
83+ "comparing a string literal without trimming the trailing newline character" ,
84+ "comparison" ,
85+ ) )
86+ } else {
87+ None
88+ } ;
89+
90+ if let Some ( ( primary_span, lint_message, operation) ) = data {
91+ span_lint_and_then ( cx, READ_LINE_WITHOUT_TRIM , primary_span, lint_message, |diag| {
92+ let local_snippet = snippet ( cx, expr. span , "<expr>" ) ;
93+
4994 diag. span_note (
5095 call. span ,
51- "call to `.read_line()` here, \
52- which leaves a trailing newline character in the buffer, \
53- which in turn will cause `.parse()` to fail",
96+ format ! (
97+ "call to `.read_line()` here, \
98+ which leaves a trailing newline character in the buffer, \
99+ which in turn will cause the {operation} to always fail"
100+ ) ,
54101 ) ;
55102
56103 diag. span_suggestion (
@@ -59,8 +106,8 @@ pub fn check(cx: &LateContext<'_>, call: &Expr<'_>, recv: &Expr<'_>, arg: &Expr<
59106 format ! ( "{local_snippet}.trim_end()" ) ,
60107 Applicability :: MachineApplicable ,
61108 ) ;
62- } ,
63- ) ;
109+ } ) ;
110+ }
64111 }
65112
66113 // only consider the first use to prevent this scenario:
0 commit comments