Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions clippy_lints/src/methods/clone_on_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,15 @@ use clippy_utils::ty::is_copy;
use rustc_errors::Applicability;
use rustc_hir::{BindingMode, ByRef, Expr, ExprKind, MatchSource, Node, PatKind, QPath};
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_middle::ty::adjustment::Adjust;
use rustc_middle::ty::print::with_forced_trimmed_paths;
use rustc_middle::ty::{self};
use rustc_span::symbol::{Symbol, sym};

use super::CLONE_ON_COPY;

/// Checks for the `CLONE_ON_COPY` lint.
#[allow(clippy::too_many_lines)]
pub(super) fn check(
cx: &LateContext<'_>,
expr: &Expr<'_>,
method_name: Symbol,
receiver: &Expr<'_>,
args: &[Expr<'_>],
) {
let arg = if method_name == sym::clone && args.is_empty() {
receiver
} else {
return;
};
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
if cx
.typeck_results()
.type_dependent_def_id(expr.hir_id)
Expand All @@ -34,10 +22,10 @@ pub(super) fn check(
{
return;
}
let arg_adjustments = cx.typeck_results().expr_adjustments(arg);
let arg_adjustments = cx.typeck_results().expr_adjustments(receiver);
let arg_ty = arg_adjustments
.last()
.map_or_else(|| cx.typeck_results().expr_ty(arg), |a| a.target);
.map_or_else(|| cx.typeck_results().expr_ty(receiver), |a| a.target);

let ty = cx.typeck_results().expr_ty(expr);
if let ty::Ref(_, inner, _) = arg_ty.kind()
Expand Down Expand Up @@ -76,7 +64,7 @@ pub(super) fn check(
};

let mut app = Applicability::MachineApplicable;
let snip = snippet_with_context(cx, arg.span, expr.span.ctxt(), "_", &mut app).0;
let snip = snippet_with_context(cx, receiver.span, expr.span.ctxt(), "_", &mut app).0;

let deref_count = arg_adjustments
.iter()
Expand Down
13 changes: 2 additions & 11 deletions clippy_lints/src/methods/clone_on_ref_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@ use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_span::symbol::{Symbol, sym};
use rustc_span::symbol::sym;

use super::CLONE_ON_REF_PTR;

pub(super) fn check(
cx: &LateContext<'_>,
expr: &hir::Expr<'_>,
method_name: Symbol,
receiver: &hir::Expr<'_>,
args: &[hir::Expr<'_>],
) {
if !(args.is_empty() && method_name == sym::clone) {
return;
}
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, receiver: &hir::Expr<'_>) {
let obj_ty = cx.typeck_results().expr_ty(receiver).peel_refs();

if let ty::Adt(adt, subst) = obj_ty.kind()
Expand Down
10 changes: 4 additions & 6 deletions clippy_lints/src/methods/expect_fun_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use clippy_utils::{contains_return, is_inside_always_const_context, peel_blocks}
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::Span;
use rustc_span::symbol::sym;
use rustc_span::{Span, Symbol};
use std::borrow::Cow;
use std::ops::ControlFlow;

Expand All @@ -20,12 +20,10 @@ pub(super) fn check<'tcx>(
format_args_storage: &FormatArgsStorage,
expr: &hir::Expr<'_>,
method_span: Span,
name: Symbol,
receiver: &'tcx hir::Expr<'tcx>,
args: &'tcx [hir::Expr<'tcx>],
) {
if name == sym::expect
&& let [arg] = args
if let [arg] = args
&& let arg_root = get_arg_root(cx, arg)
&& contains_call(cx, arg_root)
&& !contains_return(arg_root)
Expand Down Expand Up @@ -54,7 +52,7 @@ pub(super) fn check<'tcx>(
cx,
EXPECT_FUN_CALL,
span_replace_word,
format!("function call inside of `{name}`"),
"function call inside of `expect`",
"try",
format!("unwrap_or_else({closure_args} panic!({sugg}))"),
applicability,
Expand All @@ -69,7 +67,7 @@ pub(super) fn check<'tcx>(
cx,
EXPECT_FUN_CALL,
span_replace_word,
format!("function call inside of `{name}`"),
"function call inside of `expect`",
"try",
format!("unwrap_or_else({closure_args} panic!(\"{{}}\", {arg_root_snippet}))"),
applicability,
Expand Down
16 changes: 3 additions & 13 deletions clippy_lints/src/methods/inefficient_to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,12 @@ use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty};
use rustc_span::symbol::{Symbol, sym};
use rustc_span::symbol::sym;

use super::INEFFICIENT_TO_STRING;

/// Checks for the `INEFFICIENT_TO_STRING` lint
pub fn check(
cx: &LateContext<'_>,
expr: &hir::Expr<'_>,
method_name: Symbol,
receiver: &hir::Expr<'_>,
args: &[hir::Expr<'_>],
msrv: Msrv,
) {
if args.is_empty()
&& method_name == sym::to_string
&& let Some(to_string_meth_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, receiver: &hir::Expr<'_>, msrv: Msrv) {
if let Some(to_string_meth_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& cx.tcx.is_diagnostic_item(sym::to_string_method, to_string_meth_did)
&& let Some(args) = cx.typeck_results().node_args_opt(expr.hir_id)
&& let arg_ty = cx.typeck_results().expr_ty_adjusted(receiver)
Expand Down
9 changes: 1 addition & 8 deletions clippy_lints/src/methods/into_iter_on_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,9 @@ use rustc_span::symbol::{Symbol, sym};

use super::INTO_ITER_ON_REF;

pub(super) fn check(
cx: &LateContext<'_>,
expr: &hir::Expr<'_>,
method_span: Span,
method_name: Symbol,
receiver: &hir::Expr<'_>,
) {
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, method_span: Span, receiver: &hir::Expr<'_>) {
let self_ty = cx.typeck_results().expr_ty_adjusted(receiver);
if let ty::Ref(..) = self_ty.kind()
&& method_name == sym::into_iter
&& is_trait_method(cx, expr, sym::IntoIterator)
&& let Some((kind, method_name)) = ty_has_iter_method(cx, self_ty)
{
Expand Down
42 changes: 22 additions & 20 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4842,8 +4842,6 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
return;
}

self.check_methods(cx, expr);

match expr.kind {
ExprKind::Call(func, args) => {
from_iter_instead_of_collect::check(cx, expr, args, func);
Expand All @@ -4854,24 +4852,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
swap_with_temporary::check(cx, expr, func, args);
ip_constant::check(cx, expr, func, args);
},
ExprKind::MethodCall(method_call, receiver, args, _) => {
let method_span = method_call.ident.span;
or_fun_call::check(cx, expr, method_span, method_call.ident.name, receiver, args, self.msrv);
expect_fun_call::check(
cx,
&self.format_args,
expr,
method_span,
method_call.ident.name,
receiver,
args,
);
clone_on_copy::check(cx, expr, method_call.ident.name, receiver, args);
clone_on_ref_ptr::check(cx, expr, method_call.ident.name, receiver, args);
inefficient_to_string::check(cx, expr, method_call.ident.name, receiver, args, self.msrv);
single_char_add_str::check(cx, expr, receiver, args);
into_iter_on_ref::check(cx, expr, method_span, method_call.ident.name, receiver);
unnecessary_to_owned::check(cx, expr, method_call.ident.name, receiver, args, self.msrv);
ExprKind::MethodCall(..) => {
self.check_methods(cx, expr);
},
ExprKind::Binary(op, lhs, rhs) if op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne => {
let mut info = BinaryExprInfo {
Expand Down Expand Up @@ -5567,7 +5549,17 @@ impl Methods {
}
// Handle method calls whose receiver and arguments may come from expansion
if let ExprKind::MethodCall(path, recv, args, _call_span) = expr.kind {
let method_span = path.ident.span;

// Those methods do their own method name checking as they deal with multiple methods.
or_fun_call::check(cx, expr, method_span, path.ident.name, recv, args, self.msrv);
unnecessary_to_owned::check(cx, expr, path.ident.name, recv, args, self.msrv);

match (path.ident.name, args) {
(sym::clone, []) => {
clone_on_ref_ptr::check(cx, expr, recv);
clone_on_copy::check(cx, expr, recv);
},
(sym::expect, [_]) => {
unwrap_expect_used::check(
cx,
Expand All @@ -5578,6 +5570,7 @@ impl Methods {
self.allow_expect_in_tests,
unwrap_expect_used::Variant::Expect,
);
expect_fun_call::check(cx, &self.format_args, expr, method_span, recv, args);
},
(sym::expect_err, [_]) => {
unwrap_expect_used::check(
Expand All @@ -5590,6 +5583,15 @@ impl Methods {
unwrap_expect_used::Variant::Expect,
);
},
(sym::insert_str | sym::push_str, _) => {
single_char_add_str::check(cx, expr, recv, args);
},
(sym::into_iter, []) => {
into_iter_on_ref::check(cx, expr, method_span, recv);
},
(sym::to_string, []) => {
inefficient_to_string::check(cx, expr, recv, self.msrv);
},
(sym::unwrap, []) => {
unwrap_expect_used::check(
cx,
Expand Down
2 changes: 2 additions & 0 deletions clippy_utils/src/sym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ generate! {
hidden_glob_reexports,
hygiene,
insert,
insert_str,
inspect,
int_roundings,
into,
Expand Down Expand Up @@ -259,6 +260,7 @@ generate! {
powi,
product,
push,
push_str,
read,
read_exact,
read_line,
Expand Down