Skip to content

Commit 62490c4

Browse files
committed
extract conditions into modules
1 parent 3d9b45d commit 62490c4

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

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/inefficient_to_string.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::INEFFICIENT_TO_STRING;
21
use clippy_utils::diagnostics::span_lint_and_then;
32
use clippy_utils::source::snippet_with_applicability;
43
use clippy_utils::ty::{is_type_diagnostic_item, walk_ptrs_ty_depth};
@@ -8,14 +7,18 @@ use rustc_errors::Applicability;
87
use rustc_hir as hir;
98
use rustc_lint::LateContext;
109
use rustc_middle::ty::{self, Ty};
11-
use rustc_span::sym;
10+
use rustc_span::symbol::{sym, Symbol};
11+
12+
use super::INEFFICIENT_TO_STRING;
1213

1314
/// Checks for the `INEFFICIENT_TO_STRING` lint
14-
pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, arg: &hir::Expr<'_>, arg_ty: Ty<'tcx>) {
15+
pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, method_name: Symbol, args: &[hir::Expr<'_>]) {
1516
if_chain! {
17+
if args.len() == 1 && method_name == sym!(to_string);
1618
if let Some(to_string_meth_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
1719
if match_def_path(cx, to_string_meth_did, &paths::TO_STRING_METHOD);
1820
if let Some(substs) = cx.typeck_results().node_substs_opt(expr.hir_id);
21+
let arg_ty = cx.typeck_results().expr_ty_adjusted(&args[0]);
1922
let self_ty = substs.type_at(0);
2023
let (deref_self_ty, deref_count) = walk_ptrs_ty_depth(self_ty);
2124
if deref_count >= 1;
@@ -32,7 +35,7 @@ pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, arg: &hir::Expr
3235
self_ty, deref_self_ty
3336
));
3437
let mut applicability = Applicability::MachineApplicable;
35-
let arg_snippet = snippet_with_applicability(cx, arg.span, "..", &mut applicability);
38+
let arg_snippet = snippet_with_applicability(cx, args[0].span, "..", &mut applicability);
3639
diag.span_suggestion(
3740
expr.span,
3841
"try dereferencing the receiver",

clippy_lints/src/methods/mod.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod option_map_or_none;
4141
mod option_map_unwrap_or;
4242
mod or_fun_call;
4343
mod search_is_some;
44+
mod single_char_add_str;
4445
mod single_char_insert_string;
4546
mod single_char_pattern;
4647
mod single_char_push_string;
@@ -1785,23 +1786,12 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17851786
hir::ExprKind::MethodCall(ref method_call, ref method_span, ref args, _) => {
17861787
or_fun_call::check(cx, expr, *method_span, &method_call.ident.as_str(), args);
17871788
expect_fun_call::check(cx, expr, *method_span, &method_call.ident.as_str(), args);
1789+
clone_on_copy::check(cx, expr, method_call.ident.name, args);
1790+
clone_on_ref_ptr::check(cx, expr, method_call.ident.name, args);
17881791

17891792
let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0]);
1790-
if args.len() == 1 && method_call.ident.name == sym::clone {
1791-
clone_on_copy::check(cx, expr, &args[0], self_ty);
1792-
clone_on_ref_ptr::check(cx, expr, &args[0]);
1793-
}
1794-
if args.len() == 1 && method_call.ident.name == sym!(to_string) {
1795-
inefficient_to_string::check(cx, expr, &args[0], self_ty);
1796-
}
1797-
1798-
if let Some(fn_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
1799-
if match_def_path(cx, fn_def_id, &paths::PUSH_STR) {
1800-
single_char_push_string::check(cx, expr, args);
1801-
} else if match_def_path(cx, fn_def_id, &paths::INSERT_STR) {
1802-
single_char_insert_string::check(cx, expr, args);
1803-
}
1804-
}
1793+
inefficient_to_string::check(cx, expr, method_call.ident.name, args);
1794+
single_char_add_str::check(cx, expr, args);
18051795

18061796
match self_ty.kind() {
18071797
ty::Ref(_, ty, _) if *ty.kind() == ty::Str => {

0 commit comments

Comments
 (0)