Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 058d8c8

Browse files
committed
move chars_cmp_with_unwrap, chars_last_cmp and chars_next_cmp_with_unwrap to their own modules
1 parent 94fb2b5 commit 058d8c8

File tree

4 files changed

+74
-55
lines changed

4 files changed

+74
-55
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::utils::method_chain_args;
2+
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::source::snippet_with_applicability;
4+
use if_chain::if_chain;
5+
use rustc_ast::ast;
6+
use rustc_errors::Applicability;
7+
use rustc_hir as hir;
8+
use rustc_lint::LateContext;
9+
use rustc_lint::Lint;
10+
11+
/// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints with `unwrap()`.
12+
pub(super) fn check<'tcx>(
13+
cx: &LateContext<'tcx>,
14+
info: &crate::methods::BinaryExprInfo<'_>,
15+
chain_methods: &[&str],
16+
lint: &'static Lint,
17+
suggest: &str,
18+
) -> bool {
19+
if_chain! {
20+
if let Some(args) = method_chain_args(info.chain, chain_methods);
21+
if let hir::ExprKind::Lit(ref lit) = info.other.kind;
22+
if let ast::LitKind::Char(c) = lit.node;
23+
then {
24+
let mut applicability = Applicability::MachineApplicable;
25+
span_lint_and_sugg(
26+
cx,
27+
lint,
28+
info.expr.span,
29+
&format!("you should use the `{}` method", suggest),
30+
"like this",
31+
format!("{}{}.{}('{}')",
32+
if info.eq { "" } else { "!" },
33+
snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability),
34+
suggest,
35+
c),
36+
applicability,
37+
);
38+
39+
true
40+
} else {
41+
false
42+
}
43+
}
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::methods::chars_cmp;
2+
use rustc_lint::LateContext;
3+
4+
use super::CHARS_LAST_CMP;
5+
6+
/// Checks for the `CHARS_LAST_CMP` lint.
7+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool {
8+
if chars_cmp::check(cx, info, &["chars", "last"], CHARS_LAST_CMP, "ends_with") {
9+
true
10+
} else {
11+
chars_cmp::check(cx, info, &["chars", "next_back"], CHARS_LAST_CMP, "ends_with")
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use rustc_lint::LateContext;
2+
3+
use super::CHARS_NEXT_CMP;
4+
5+
/// Checks for the `CHARS_NEXT_CMP` lint with `unwrap()`.
6+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool {
7+
crate::methods::chars_cmp_with_unwrap::check(cx, info, &["chars", "next", "unwrap"], CHARS_NEXT_CMP, "starts_with")
8+
}

clippy_lints/src/methods/mod.rs

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
mod bind_instead_of_map;
22
mod bytes_nth;
33
mod chars_cmp;
4+
mod chars_cmp_with_unwrap;
5+
mod chars_last_cmp;
46
mod chars_next_cmp;
7+
mod chars_next_cmp_with_unwrap;
58
mod clone_on_copy;
69
mod clone_on_ref_ptr;
710
mod expect_fun_call;
@@ -54,7 +57,7 @@ mod wrong_self_convention;
5457
mod zst_offset;
5558

5659
use bind_instead_of_map::BindInsteadOfMap;
57-
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg};
60+
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
5861
use clippy_utils::source::snippet_with_applicability;
5962
use clippy_utils::ty::{contains_ty, implements_trait, is_copy, is_type_diagnostic_item};
6063
use clippy_utils::{
@@ -66,7 +69,7 @@ use rustc_ast::ast;
6669
use rustc_errors::Applicability;
6770
use rustc_hir as hir;
6871
use rustc_hir::{TraitItem, TraitItemKind};
69-
use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
72+
use rustc_lint::{LateContext, LateLintPass, LintContext};
7073
use rustc_middle::lint::in_external_macro;
7174
use rustc_middle::ty::{self, TraitRef, Ty, TyS};
7275
use rustc_semver::RustcVersion;
@@ -2037,66 +2040,17 @@ fn lint_binary_expr_with_method_call(cx: &LateContext<'_>, info: &mut BinaryExpr
20372040
}
20382041

20392042
lint_with_both_lhs_and_rhs!(chars_next_cmp::check, cx, info);
2040-
lint_with_both_lhs_and_rhs!(lint_chars_last_cmp, cx, info);
2041-
lint_with_both_lhs_and_rhs!(lint_chars_next_cmp_with_unwrap, cx, info);
2043+
lint_with_both_lhs_and_rhs!(chars_last_cmp::check, cx, info);
2044+
lint_with_both_lhs_and_rhs!(chars_next_cmp_with_unwrap::check, cx, info);
20422045
lint_with_both_lhs_and_rhs!(lint_chars_last_cmp_with_unwrap, cx, info);
20432046
}
20442047

2045-
/// Checks for the `CHARS_LAST_CMP` lint.
2046-
fn lint_chars_last_cmp<'tcx>(cx: &LateContext<'tcx>, info: &BinaryExprInfo<'_>) -> bool {
2047-
if chars_cmp::check(cx, info, &["chars", "last"], CHARS_LAST_CMP, "ends_with") {
2048-
true
2049-
} else {
2050-
chars_cmp::check(cx, info, &["chars", "next_back"], CHARS_LAST_CMP, "ends_with")
2051-
}
2052-
}
2053-
2054-
/// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints with `unwrap()`.
2055-
fn lint_chars_cmp_with_unwrap<'tcx>(
2056-
cx: &LateContext<'tcx>,
2057-
info: &BinaryExprInfo<'_>,
2058-
chain_methods: &[&str],
2059-
lint: &'static Lint,
2060-
suggest: &str,
2061-
) -> bool {
2062-
if_chain! {
2063-
if let Some(args) = method_chain_args(info.chain, chain_methods);
2064-
if let hir::ExprKind::Lit(ref lit) = info.other.kind;
2065-
if let ast::LitKind::Char(c) = lit.node;
2066-
then {
2067-
let mut applicability = Applicability::MachineApplicable;
2068-
span_lint_and_sugg(
2069-
cx,
2070-
lint,
2071-
info.expr.span,
2072-
&format!("you should use the `{}` method", suggest),
2073-
"like this",
2074-
format!("{}{}.{}('{}')",
2075-
if info.eq { "" } else { "!" },
2076-
snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability),
2077-
suggest,
2078-
c),
2079-
applicability,
2080-
);
2081-
2082-
true
2083-
} else {
2084-
false
2085-
}
2086-
}
2087-
}
2088-
2089-
/// Checks for the `CHARS_NEXT_CMP` lint with `unwrap()`.
2090-
fn lint_chars_next_cmp_with_unwrap<'tcx>(cx: &LateContext<'tcx>, info: &BinaryExprInfo<'_>) -> bool {
2091-
lint_chars_cmp_with_unwrap(cx, info, &["chars", "next", "unwrap"], CHARS_NEXT_CMP, "starts_with")
2092-
}
2093-
20942048
/// Checks for the `CHARS_LAST_CMP` lint with `unwrap()`.
20952049
fn lint_chars_last_cmp_with_unwrap<'tcx>(cx: &LateContext<'tcx>, info: &BinaryExprInfo<'_>) -> bool {
2096-
if lint_chars_cmp_with_unwrap(cx, info, &["chars", "last", "unwrap"], CHARS_LAST_CMP, "ends_with") {
2050+
if chars_cmp_with_unwrap::check(cx, info, &["chars", "last", "unwrap"], CHARS_LAST_CMP, "ends_with") {
20972051
true
20982052
} else {
2099-
lint_chars_cmp_with_unwrap(cx, info, &["chars", "next_back", "unwrap"], CHARS_LAST_CMP, "ends_with")
2053+
chars_cmp_with_unwrap::check(cx, info, &["chars", "next_back", "unwrap"], CHARS_LAST_CMP, "ends_with")
21002054
}
21012055
}
21022056

0 commit comments

Comments
 (0)