mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-25 03:47:18 +00:00
Ignore trait implementations
This commit is contained in:
parent
4c8d248190
commit
4e5c02e898
3 changed files with 23 additions and 12 deletions
|
@ -1,11 +1,11 @@
|
||||||
use crate::utils::{
|
use crate::utils::{
|
||||||
in_macro, is_type_diagnostic_item, match_path, match_qpath, paths, return_ty, snippet, span_lint_and_then,
|
in_macro, is_type_diagnostic_item, match_qpath, paths, return_ty, snippet, span_lint_and_then,
|
||||||
trait_ref_of_method, visitors::find_all_ret_expressions,
|
visitors::find_all_ret_expressions,
|
||||||
};
|
};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::intravisit::FnKind;
|
use rustc_hir::intravisit::FnKind;
|
||||||
use rustc_hir::{Body, ExprKind, FnDecl, HirId};
|
use rustc_hir::{Body, ExprKind, FnDecl, HirId, ItemKind, Node};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::ty::subst::GenericArgKind;
|
use rustc_middle::ty::subst::GenericArgKind;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
|
@ -63,14 +63,6 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
|
||||||
span: Span,
|
span: Span,
|
||||||
hir_id: HirId,
|
hir_id: HirId,
|
||||||
) {
|
) {
|
||||||
if_chain! {
|
|
||||||
if let Some(trait_ref) = trait_ref_of_method(cx, hir_id);
|
|
||||||
if match_path(trait_ref.path, &paths::PARTIAL_ORD);
|
|
||||||
then {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match fn_kind {
|
match fn_kind {
|
||||||
FnKind::ItemFn(.., visibility, _) | FnKind::Method(.., Some(visibility), _) => {
|
FnKind::ItemFn(.., visibility, _) | FnKind::Method(.., Some(visibility), _) => {
|
||||||
if visibility.node.is_pub() {
|
if visibility.node.is_pub() {
|
||||||
|
@ -81,6 +73,12 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
|
||||||
|
if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), ..} | ItemKind::Trait(..)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let (return_type, path) = if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(option_type)) {
|
let (return_type, path) = if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(option_type)) {
|
||||||
("Option", &paths::OPTION_SOME)
|
("Option", &paths::OPTION_SOME)
|
||||||
} else if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(result_type)) {
|
} else if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(result_type)) {
|
||||||
|
|
|
@ -81,7 +81,6 @@ pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to
|
||||||
pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
|
pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
|
||||||
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"];
|
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"];
|
||||||
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];
|
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];
|
||||||
pub const PARTIAL_ORD: [&str; 3] = ["std", "cmp", "PartialOrd"];
|
|
||||||
pub const PATH: [&str; 3] = ["std", "path", "Path"];
|
pub const PATH: [&str; 3] = ["std", "path", "Path"];
|
||||||
pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
|
pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
|
||||||
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
|
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
|
||||||
|
|
|
@ -95,6 +95,20 @@ impl A {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait B {
|
||||||
|
// trait impls are not linted
|
||||||
|
fn func13() -> Option<i32> {
|
||||||
|
Some(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl A for B {
|
||||||
|
// trait impls are not linted
|
||||||
|
fn func13() -> Option<i32> {
|
||||||
|
Some(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// method calls are not linted
|
// method calls are not linted
|
||||||
func1(true, true);
|
func1(true, true);
|
||||||
|
|
Loading…
Add table
Reference in a new issue