@@ -3,46 +3,13 @@ use clippy_utils::ty::is_type_diagnostic_item;
33use rustc_ast:: ast:: LitKind ;
44use rustc_errors:: Applicability ;
55use rustc_hir:: intravisit:: { walk_expr, Visitor } ;
6- use rustc_hir:: { Arm , Expr , ExprKind , MatchSource , PatKind } ;
7- use rustc_lint:: { LateContext , LateLintPass } ;
8- use rustc_middle:: lint:: in_external_macro;
6+ use rustc_hir:: { Arm , Expr , ExprKind , PatKind } ;
7+ use rustc_lint:: LateContext ;
98use rustc_middle:: ty;
10- use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
119use rustc_span:: symbol:: Symbol ;
1210use rustc_span:: { sym, Span } ;
1311
14- declare_clippy_lint ! {
15- /// ### What it does
16- /// Checks for `match` expressions modifying the case of a string with non-compliant arms
17- ///
18- /// ### Why is this bad?
19- /// The arm is unreachable, which is likely a mistake
20- ///
21- /// ### Example
22- /// ```rust
23- /// # let text = "Foo";
24- /// match &*text.to_ascii_lowercase() {
25- /// "foo" => {},
26- /// "Bar" => {},
27- /// _ => {},
28- /// }
29- /// ```
30- /// Use instead:
31- /// ```rust
32- /// # let text = "Foo";
33- /// match &*text.to_ascii_lowercase() {
34- /// "foo" => {},
35- /// "bar" => {},
36- /// _ => {},
37- /// }
38- /// ```
39- #[ clippy:: version = "1.58.0" ]
40- pub MATCH_STR_CASE_MISMATCH ,
41- correctness,
42- "creation of a case altering match expression with non-compliant arms"
43- }
44-
45- declare_lint_pass ! ( MatchStrCaseMismatch => [ MATCH_STR_CASE_MISMATCH ] ) ;
12+ use super :: MATCH_STR_CASE_MISMATCH ;
4613
4714#[ derive( Debug ) ]
4815enum CaseMethod {
@@ -52,25 +19,21 @@ enum CaseMethod {
5219 AsciiUppercase ,
5320}
5421
55- impl < ' tcx > LateLintPass < ' tcx > for MatchStrCaseMismatch {
56- fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
57- if_chain ! {
58- if !in_external_macro( cx. tcx. sess, expr. span) ;
59- if let ExprKind :: Match ( match_expr, arms, MatchSource :: Normal ) = expr. kind;
60- if let ty:: Ref ( _, ty, _) = cx. typeck_results( ) . expr_ty( match_expr) . kind( ) ;
61- if let ty:: Str = ty. kind( ) ;
62- then {
63- let mut visitor = MatchExprVisitor {
64- cx,
65- case_method: None ,
66- } ;
67-
68- visitor. visit_expr( match_expr) ;
69-
70- if let Some ( case_method) = visitor. case_method {
71- if let Some ( ( bad_case_span, bad_case_sym) ) = verify_case( & case_method, arms) {
72- lint( cx, & case_method, bad_case_span, bad_case_sym. as_str( ) ) ;
73- }
22+ pub ( super ) fn check < ' tcx > ( cx : & LateContext < ' tcx > , scrutinee : & ' tcx Expr < ' _ > , arms : & ' tcx [ Arm < ' _ > ] ) {
23+ if_chain ! {
24+ if let ty:: Ref ( _, ty, _) = cx. typeck_results( ) . expr_ty( scrutinee) . kind( ) ;
25+ if let ty:: Str = ty. kind( ) ;
26+ then {
27+ let mut visitor = MatchExprVisitor {
28+ cx,
29+ case_method: None ,
30+ } ;
31+
32+ visitor. visit_expr( scrutinee) ;
33+
34+ if let Some ( case_method) = visitor. case_method {
35+ if let Some ( ( bad_case_span, bad_case_sym) ) = verify_case( & case_method, arms) {
36+ lint( cx, & case_method, bad_case_span, bad_case_sym. as_str( ) ) ;
7437 }
7538 }
7639 }
0 commit comments