Skip to content

Commit 8c8a52e

Browse files
committed
Move MatchStrCaseMismatch into Matches lint pass
1 parent 3d8d734 commit 8c8a52e

File tree

6 files changed

+55
-60
lines changed

6 files changed

+55
-60
lines changed

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
140140
LintId::of(map_unit_fn::OPTION_MAP_UNIT_FN),
141141
LintId::of(map_unit_fn::RESULT_MAP_UNIT_FN),
142142
LintId::of(match_result_ok::MATCH_RESULT_OK),
143-
LintId::of(match_str_case_mismatch::MATCH_STR_CASE_MISMATCH),
144143
LintId::of(matches::COLLAPSIBLE_MATCH),
145144
LintId::of(matches::INFALLIBLE_DESTRUCTURING_MATCH),
146145
LintId::of(matches::MANUAL_UNWRAP_OR),
@@ -149,6 +148,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
149148
LintId::of(matches::MATCH_OVERLAPPING_ARM),
150149
LintId::of(matches::MATCH_REF_PATS),
151150
LintId::of(matches::MATCH_SINGLE_BINDING),
151+
LintId::of(matches::MATCH_STR_CASE_MISMATCH),
152152
LintId::of(matches::NEEDLESS_MATCH),
153153
LintId::of(matches::REDUNDANT_PATTERN_MATCHING),
154154
LintId::of(matches::SINGLE_MATCH),

clippy_lints/src/lib.register_correctness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
3939
LintId::of(loops::ITER_NEXT_LOOP),
4040
LintId::of(loops::NEVER_LOOP),
4141
LintId::of(loops::WHILE_IMMUTABLE_CONDITION),
42-
LintId::of(match_str_case_mismatch::MATCH_STR_CASE_MISMATCH),
42+
LintId::of(matches::MATCH_STR_CASE_MISMATCH),
4343
LintId::of(mem_replace::MEM_REPLACE_WITH_UNINIT),
4444
LintId::of(methods::CLONE_DOUBLE_REF),
4545
LintId::of(methods::ITERATOR_STEP_BY_ZERO),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ store.register_lints(&[
259259
map_unit_fn::OPTION_MAP_UNIT_FN,
260260
map_unit_fn::RESULT_MAP_UNIT_FN,
261261
match_result_ok::MATCH_RESULT_OK,
262-
match_str_case_mismatch::MATCH_STR_CASE_MISMATCH,
263262
matches::COLLAPSIBLE_MATCH,
264263
matches::INFALLIBLE_DESTRUCTURING_MATCH,
265264
matches::MANUAL_UNWRAP_OR,
@@ -271,6 +270,7 @@ store.register_lints(&[
271270
matches::MATCH_REF_PATS,
272271
matches::MATCH_SAME_ARMS,
273272
matches::MATCH_SINGLE_BINDING,
273+
matches::MATCH_STR_CASE_MISMATCH,
274274
matches::MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
275275
matches::MATCH_WILD_ERR_ARM,
276276
matches::NEEDLESS_MATCH,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ mod map_clone;
287287
mod map_err_ignore;
288288
mod map_unit_fn;
289289
mod match_result_ok;
290-
mod match_str_case_mismatch;
291290
mod matches;
292291
mod mem_forget;
293292
mod mem_replace;
@@ -875,7 +874,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
875874
))
876875
});
877876
store.register_late_pass(move || Box::new(undocumented_unsafe_blocks::UndocumentedUnsafeBlocks));
878-
store.register_late_pass(|| Box::new(match_str_case_mismatch::MatchStrCaseMismatch));
879877
store.register_late_pass(move || Box::new(format_args::FormatArgs));
880878
store.register_late_pass(|| Box::new(trailing_empty_array::TrailingEmptyArray));
881879
store.register_early_pass(|| Box::new(octal_escapes::OctalEscapes));

clippy_lints/src/match_str_case_mismatch.rs renamed to clippy_lints/src/matches/match_str_case_mismatch.rs

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,13 @@ use clippy_utils::ty::is_type_diagnostic_item;
33
use rustc_ast::ast::LitKind;
44
use rustc_errors::Applicability;
55
use 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;
98
use rustc_middle::ty;
10-
use rustc_session::{declare_lint_pass, declare_tool_lint};
119
use rustc_span::symbol::Symbol;
1210
use 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)]
4815
enum 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
}

clippy_lints/src/matches/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod match_on_vec_items;
1818
mod match_ref_pats;
1919
mod match_same_arms;
2020
mod match_single_binding;
21+
mod match_str_case_mismatch;
2122
mod match_wild_enum;
2223
mod match_wild_err_arm;
2324
mod needless_match;
@@ -716,6 +717,37 @@ declare_clippy_lint! {
716717
"matching on vector elements can panic"
717718
}
718719

720+
declare_clippy_lint! {
721+
/// ### What it does
722+
/// Checks for `match` expressions modifying the case of a string with non-compliant arms
723+
///
724+
/// ### Why is this bad?
725+
/// The arm is unreachable, which is likely a mistake
726+
///
727+
/// ### Example
728+
/// ```rust
729+
/// # let text = "Foo";
730+
/// match &*text.to_ascii_lowercase() {
731+
/// "foo" => {},
732+
/// "Bar" => {},
733+
/// _ => {},
734+
/// }
735+
/// ```
736+
/// Use instead:
737+
/// ```rust
738+
/// # let text = "Foo";
739+
/// match &*text.to_ascii_lowercase() {
740+
/// "foo" => {},
741+
/// "bar" => {},
742+
/// _ => {},
743+
/// }
744+
/// ```
745+
#[clippy::version = "1.58.0"]
746+
pub MATCH_STR_CASE_MISMATCH,
747+
correctness,
748+
"creation of a case altering match expression with non-compliant arms"
749+
}
750+
719751
#[derive(Default)]
720752
pub struct Matches {
721753
msrv: Option<RustcVersion>,
@@ -753,6 +785,7 @@ impl_lint_pass!(Matches => [
753785
COLLAPSIBLE_MATCH,
754786
MANUAL_UNWRAP_OR,
755787
MATCH_ON_VEC_ITEMS,
788+
MATCH_STR_CASE_MISMATCH,
756789
]);
757790

758791
impl<'tcx> LateLintPass<'tcx> for Matches {
@@ -790,6 +823,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
790823
match_as_ref::check(cx, ex, arms, expr);
791824
needless_match::check_match(cx, ex, arms, expr);
792825
match_on_vec_items::check(cx, ex);
826+
match_str_case_mismatch::check(cx, ex, arms);
793827

794828
if !in_constant(cx, expr.hir_id) {
795829
manual_unwrap_or::check(cx, expr, ex, arms);

0 commit comments

Comments
 (0)