Skip to content

Commit 4e5c02e

Browse files
committed
Ignore trait implementations
1 parent 4c8d248 commit 4e5c02e

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

clippy_lints/src/unnecessary_wrap.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::utils::{
2-
in_macro, is_type_diagnostic_item, match_path, match_qpath, paths, return_ty, snippet, span_lint_and_then,
3-
trait_ref_of_method, visitors::find_all_ret_expressions,
2+
in_macro, is_type_diagnostic_item, match_qpath, paths, return_ty, snippet, span_lint_and_then,
3+
visitors::find_all_ret_expressions,
44
};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::intravisit::FnKind;
8-
use rustc_hir::{Body, ExprKind, FnDecl, HirId};
8+
use rustc_hir::{Body, ExprKind, FnDecl, HirId, ItemKind, Node};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::ty::subst::GenericArgKind;
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -63,14 +63,6 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
6363
span: Span,
6464
hir_id: HirId,
6565
) {
66-
if_chain! {
67-
if let Some(trait_ref) = trait_ref_of_method(cx, hir_id);
68-
if match_path(trait_ref.path, &paths::PARTIAL_ORD);
69-
then {
70-
return;
71-
}
72-
}
73-
7466
match fn_kind {
7567
FnKind::ItemFn(.., visibility, _) | FnKind::Method(.., Some(visibility), _) => {
7668
if visibility.node.is_pub() {
@@ -81,6 +73,12 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
8173
_ => (),
8274
}
8375

76+
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
77+
if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), ..} | ItemKind::Trait(..)) {
78+
return;
79+
}
80+
}
81+
8482
let (return_type, path) = if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(option_type)) {
8583
("Option", &paths::OPTION_SOME)
8684
} else if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(result_type)) {

clippy_lints/src/utils/paths.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to
8181
pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
8282
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"];
8383
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];
84-
pub const PARTIAL_ORD: [&str; 3] = ["std", "cmp", "PartialOrd"];
8584
pub const PATH: [&str; 3] = ["std", "path", "Path"];
8685
pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
8786
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];

tests/ui/unnecessary_wrap.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ impl A {
9595
}
9696
}
9797

98+
trait B {
99+
// trait impls are not linted
100+
fn func13() -> Option<i32> {
101+
Some(1)
102+
}
103+
}
104+
105+
impl A for B {
106+
// trait impls are not linted
107+
fn func13() -> Option<i32> {
108+
Some(0)
109+
}
110+
}
111+
98112
fn main() {
99113
// method calls are not linted
100114
func1(true, true);

0 commit comments

Comments
 (0)