-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
327 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use clippy_utils::{in_macro, is_automatically_derived, is_default, match_def_path, paths}; | ||
use rustc_hir::{Block, Expr, ExprField, ExprKind, Impl, ImplItemKind, Item, ItemKind, Node, QPath}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for double comparisons that could be simplified to a single expression. | ||
/// | ||
/// | ||
/// ### Why is this bad? | ||
/// Readability. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// # let x = 1; | ||
/// # let y = 2; | ||
/// if x == y || x < y {} | ||
/// ``` | ||
/// | ||
/// Could be written as: | ||
/// | ||
/// ```rust | ||
/// # let x = 1; | ||
/// # let y = 2; | ||
/// if x <= y {} | ||
/// ``` | ||
pub DERIVABLE_IMPLS, | ||
complexity, | ||
"manual implementation of a trait which is equal to a derive" | ||
} | ||
|
||
declare_lint_pass!(DerivableImpls => [DERIVABLE_IMPLS]); | ||
|
||
fn struct_fields<'hir>(e: &'hir Expr<'hir>) -> Option<&'hir [ExprField<'hir>]> { | ||
match e.kind { | ||
ExprKind::Struct(_, fields, _) => Some(fields), | ||
ExprKind::Block(Block { expr, .. }, _) => expr.and_then(struct_fields), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn is_path_self(e: &Expr<'_>) -> bool { | ||
if let ExprKind::Path(QPath::Resolved(_, p)) = e.kind { | ||
if p.segments.len() != 1 { | ||
return false; | ||
} | ||
if p.segments[0].ident.name == rustc_span::symbol::kw::SelfUpper { | ||
return true; | ||
} | ||
} | ||
false | ||
} | ||
|
||
fn tuple_fields<'hir>(e: &'hir Expr<'hir>) -> Option<&'hir [Expr<'hir>]> { | ||
match e.kind { | ||
ExprKind::Tup(fields) => Some(fields), | ||
ExprKind::Call(callee, args) => { | ||
if is_path_self(callee) { | ||
Some(args) | ||
} else { | ||
None | ||
} | ||
}, | ||
ExprKind::Block(Block { expr, .. }, _) => expr.and_then(tuple_fields), | ||
_ => None, | ||
} | ||
} | ||
|
||
impl<'tcx> LateLintPass<'tcx> for DerivableImpls { | ||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { | ||
if let ItemKind::Impl(Impl { | ||
of_trait: Some(ref trait_ref), | ||
items, | ||
.. | ||
}) = item.kind | ||
{ | ||
let attrs = cx.tcx.hir().attrs(item.hir_id()); | ||
if is_automatically_derived(attrs) { | ||
return; | ||
} | ||
if in_macro(item.span) { | ||
return; | ||
} | ||
if let Some(def_id) = trait_ref.trait_def_id() { | ||
if !match_def_path(cx, def_id, &paths::DEFAULT_TRAIT) { | ||
return; | ||
} | ||
} | ||
let impl_item_hir = items[0].id.hir_id(); | ||
let func_expr = match cx.tcx.hir().find(impl_item_hir) { | ||
Some(Node::ImplItem(x)) => match &x.kind { | ||
ImplItemKind::Fn(_, b) => match cx.tcx.hir().find(b.hir_id) { | ||
Some(Node::Expr(e)) => e, | ||
_ => return, | ||
}, | ||
_ => return, | ||
}, | ||
_ => return, | ||
}; | ||
let should_emit = if let Some(s) = struct_fields(func_expr) { | ||
s.iter().all(|ef| is_default(cx, ef.expr)) | ||
} else if let Some(s) = tuple_fields(func_expr) { | ||
s.iter().all(|e| is_default(cx, e)) | ||
} else { | ||
return; | ||
}; | ||
if should_emit { | ||
span_lint(cx, DERIVABLE_IMPLS, item.span, "derivable impl of trait Default:"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.