Skip to content

Commit 029777f

Browse files
committed
Auto merge of #6896 - TaKO8Ki:refactor-lints-in-methods-module, r=phansch
Refactor lints in methods module This PR refactors methods lints other than the lints I refactored in #6826 and moves some functions to methods/utils.rs. Basically, I follow the instruction described in #6680. **For ease of review, I refactored step by step, keeping each commit small.** closes #6886 cc: `@phansch,` `@flip1995,` `@Y-Nak` changelog: Move lints in methods module to their own modules and some function to methods/utils.rs.
2 parents aca95aa + b6a2757 commit 029777f

27 files changed

+388
-333
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 clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet_with_applicability;
3+
use clippy_utils::{method_chain_args, single_segment_path};
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::method_chain_args;
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::methods::chars_cmp_with_unwrap;
2+
use rustc_lint::LateContext;
3+
4+
use super::CHARS_LAST_CMP;
5+
6+
/// Checks for the `CHARS_LAST_CMP` lint with `unwrap()`.
7+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, info: &crate::methods::BinaryExprInfo<'_>) -> bool {
8+
if chars_cmp_with_unwrap::check(cx, info, &["chars", "last", "unwrap"], CHARS_LAST_CMP, "ends_with") {
9+
true
10+
} else {
11+
chars_cmp_with_unwrap::check(cx, info, &["chars", "next_back", "unwrap"], 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.
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+
}
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/clone_on_copy.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ use clippy_utils::ty::is_copy;
44
use rustc_errors::Applicability;
55
use rustc_hir as hir;
66
use rustc_lint::LateContext;
7-
use rustc_middle::ty::{self, Ty};
7+
use rustc_middle::ty;
8+
use rustc_span::symbol::{sym, Symbol};
89
use std::iter;
910

1011
use super::CLONE_DOUBLE_REF;
1112
use super::CLONE_ON_COPY;
1213

1314
/// Checks for the `CLONE_ON_COPY` lint.
14-
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Expr<'_>, arg_ty: Ty<'_>) {
15+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, method_name: Symbol, args: &[hir::Expr<'_>]) {
16+
if !(args.len() == 1 && method_name == sym::clone) {
17+
return;
18+
}
19+
let arg = &args[0];
20+
let arg_ty = cx.typeck_results().expr_ty_adjusted(&args[0]);
1521
let ty = cx.typeck_results().expr_ty(expr);
1622
if let ty::Ref(_, inner, _) = arg_ty.kind() {
1723
if let ty::Ref(_, innermost, _) = inner.kind() {

clippy_lints/src/methods/clone_on_ref_ptr.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ use rustc_errors::Applicability;
66
use rustc_hir as hir;
77
use rustc_lint::LateContext;
88
use rustc_middle::ty;
9-
use rustc_span::symbol::sym;
9+
use rustc_span::symbol::{sym, Symbol};
1010

1111
use super::CLONE_ON_REF_PTR;
1212

13-
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
13+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, method_name: Symbol, args: &[hir::Expr<'_>]) {
14+
if !(args.len() == 1 && method_name == sym::clone) {
15+
return;
16+
}
17+
let arg = &args[0];
1418
let obj_ty = cx.typeck_results().expr_ty(arg).peel_refs();
1519

1620
if let ty::Adt(_, subst) = obj_ty.kind() {

clippy_lints/src/methods/filter_flat_map.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ use rustc_span::sym;
77
use super::FILTER_MAP;
88

99
/// lint use of `filter().flat_map()` for `Iterators`
10-
pub(super) fn check<'tcx>(
11-
cx: &LateContext<'tcx>,
12-
expr: &'tcx hir::Expr<'_>,
13-
_filter_args: &'tcx [hir::Expr<'_>],
14-
_map_args: &'tcx [hir::Expr<'_>],
15-
) {
10+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
1611
// lint if caller of `.filter().flat_map()` is an Iterator
1712
if is_trait_method(cx, expr, sym::Iterator) {
1813
let msg = "called `filter(..).flat_map(..)` on an `Iterator`";

clippy_lints/src/methods/filter_map_flat_map.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ use rustc_span::sym;
77
use super::FILTER_MAP;
88

99
/// lint use of `filter_map().flat_map()` for `Iterators`
10-
pub(super) fn check<'tcx>(
11-
cx: &LateContext<'tcx>,
12-
expr: &'tcx hir::Expr<'_>,
13-
_filter_args: &'tcx [hir::Expr<'_>],
14-
_map_args: &'tcx [hir::Expr<'_>],
15-
) {
10+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
1611
// lint if caller of `.filter_map().flat_map()` is an Iterator
1712
if is_trait_method(cx, expr, sym::Iterator) {
1813
let msg = "called `filter_map(..).flat_map(..)` on an `Iterator`";

0 commit comments

Comments
 (0)