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

Commit 94fb2b5

Browse files
committed
move chars_cmp and chars_next_cmp to its own modules
1 parent 6595d55 commit 94fb2b5

File tree

3 files changed

+68
-53
lines changed

3 files changed

+68
-53
lines changed

clippy_lints/src/methods/chars_cmp.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use crate::utils::{method_chain_args, single_segment_path};
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_errors::Applicability;
6+
use rustc_hir as hir;
7+
use rustc_lint::LateContext;
8+
use rustc_lint::Lint;
9+
use rustc_middle::ty;
10+
use rustc_span::sym;
11+
12+
/// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints.
13+
pub(super) fn check(
14+
cx: &LateContext<'_>,
15+
info: &crate::methods::BinaryExprInfo<'_>,
16+
chain_methods: &[&str],
17+
lint: &'static Lint,
18+
suggest: &str,
19+
) -> bool {
20+
if_chain! {
21+
if let Some(args) = method_chain_args(info.chain, chain_methods);
22+
if let hir::ExprKind::Call(ref fun, ref arg_char) = info.other.kind;
23+
if arg_char.len() == 1;
24+
if let hir::ExprKind::Path(ref qpath) = fun.kind;
25+
if let Some(segment) = single_segment_path(qpath);
26+
if segment.ident.name == sym::Some;
27+
then {
28+
let mut applicability = Applicability::MachineApplicable;
29+
let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0][0]).peel_refs();
30+
31+
if *self_ty.kind() != ty::Str {
32+
return false;
33+
}
34+
35+
span_lint_and_sugg(
36+
cx,
37+
lint,
38+
info.expr.span,
39+
&format!("you should use the `{}` method", suggest),
40+
"like this",
41+
format!("{}{}.{}({})",
42+
if info.eq { "" } else { "!" },
43+
snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability),
44+
suggest,
45+
snippet_with_applicability(cx, arg_char[0].span, "..", &mut applicability)),
46+
applicability,
47+
);
48+
49+
return true;
50+
}
51+
}
52+
53+
false
54+
}
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.
6+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool {
7+
crate::methods::chars_cmp::check(cx, info, &["chars", "next"], CHARS_NEXT_CMP, "starts_with")
8+
}

clippy_lints/src/methods/mod.rs

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod bind_instead_of_map;
22
mod bytes_nth;
3+
mod chars_cmp;
4+
mod chars_next_cmp;
35
mod clone_on_copy;
46
mod clone_on_ref_ptr;
57
mod expect_fun_call;
@@ -2024,7 +2026,7 @@ struct BinaryExprInfo<'a> {
20242026
/// Checks for the `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints.
20252027
fn lint_binary_expr_with_method_call(cx: &LateContext<'_>, info: &mut BinaryExprInfo<'_>) {
20262028
macro_rules! lint_with_both_lhs_and_rhs {
2027-
($func:ident, $cx:expr, $info:ident) => {
2029+
($func:expr, $cx:expr, $info:ident) => {
20282030
if !$func($cx, $info) {
20292031
::std::mem::swap(&mut $info.chain, &mut $info.other);
20302032
if $func($cx, $info) {
@@ -2034,67 +2036,18 @@ fn lint_binary_expr_with_method_call(cx: &LateContext<'_>, info: &mut BinaryExpr
20342036
};
20352037
}
20362038

2037-
lint_with_both_lhs_and_rhs!(lint_chars_next_cmp, cx, info);
2039+
lint_with_both_lhs_and_rhs!(chars_next_cmp::check, cx, info);
20382040
lint_with_both_lhs_and_rhs!(lint_chars_last_cmp, cx, info);
20392041
lint_with_both_lhs_and_rhs!(lint_chars_next_cmp_with_unwrap, cx, info);
20402042
lint_with_both_lhs_and_rhs!(lint_chars_last_cmp_with_unwrap, cx, info);
20412043
}
20422044

2043-
/// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints.
2044-
fn lint_chars_cmp(
2045-
cx: &LateContext<'_>,
2046-
info: &BinaryExprInfo<'_>,
2047-
chain_methods: &[&str],
2048-
lint: &'static Lint,
2049-
suggest: &str,
2050-
) -> bool {
2051-
if_chain! {
2052-
if let Some(args) = method_chain_args(info.chain, chain_methods);
2053-
if let hir::ExprKind::Call(ref fun, ref arg_char) = info.other.kind;
2054-
if arg_char.len() == 1;
2055-
if let hir::ExprKind::Path(ref qpath) = fun.kind;
2056-
if let Some(segment) = single_segment_path(qpath);
2057-
if segment.ident.name == sym::Some;
2058-
then {
2059-
let mut applicability = Applicability::MachineApplicable;
2060-
let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0][0]).peel_refs();
2061-
2062-
if *self_ty.kind() != ty::Str {
2063-
return false;
2064-
}
2065-
2066-
span_lint_and_sugg(
2067-
cx,
2068-
lint,
2069-
info.expr.span,
2070-
&format!("you should use the `{}` method", suggest),
2071-
"like this",
2072-
format!("{}{}.{}({})",
2073-
if info.eq { "" } else { "!" },
2074-
snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability),
2075-
suggest,
2076-
snippet_with_applicability(cx, arg_char[0].span, "..", &mut applicability)),
2077-
applicability,
2078-
);
2079-
2080-
return true;
2081-
}
2082-
}
2083-
2084-
false
2085-
}
2086-
2087-
/// Checks for the `CHARS_NEXT_CMP` lint.
2088-
fn lint_chars_next_cmp<'tcx>(cx: &LateContext<'tcx>, info: &BinaryExprInfo<'_>) -> bool {
2089-
lint_chars_cmp(cx, info, &["chars", "next"], CHARS_NEXT_CMP, "starts_with")
2090-
}
2091-
20922045
/// Checks for the `CHARS_LAST_CMP` lint.
20932046
fn lint_chars_last_cmp<'tcx>(cx: &LateContext<'tcx>, info: &BinaryExprInfo<'_>) -> bool {
2094-
if lint_chars_cmp(cx, info, &["chars", "last"], CHARS_LAST_CMP, "ends_with") {
2047+
if chars_cmp::check(cx, info, &["chars", "last"], CHARS_LAST_CMP, "ends_with") {
20952048
true
20962049
} else {
2097-
lint_chars_cmp(cx, info, &["chars", "next_back"], CHARS_LAST_CMP, "ends_with")
2050+
chars_cmp::check(cx, info, &["chars", "next_back"], CHARS_LAST_CMP, "ends_with")
20982051
}
20992052
}
21002053

0 commit comments

Comments
 (0)