2017-09-05 04:33:04 -05:00
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2017-07-28 06:28:07 -05:00
|
|
|
use rustc::hir::*;
|
2017-09-05 04:33:04 -05:00
|
|
|
use rustc::hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
|
2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::{in_macro, span_lint_and_then};
|
2017-07-28 06:28:07 -05:00
|
|
|
use syntax::ast::NodeId;
|
2017-08-18 12:42:26 -05:00
|
|
|
use syntax_pos::symbol::keywords::SelfType;
|
2017-07-28 06:28:07 -05:00
|
|
|
|
|
|
|
/// **What it does:** Checks for unnecessary repetition of structure name when a
|
|
|
|
/// replacement with `Self` is applicable.
|
|
|
|
///
|
2017-09-03 16:15:15 -05:00
|
|
|
/// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct
|
|
|
|
/// name
|
2017-07-28 06:28:07 -05:00
|
|
|
/// feels inconsistent.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// struct Foo {}
|
|
|
|
/// impl Foo {
|
|
|
|
/// fn new() -> Foo {
|
|
|
|
/// Foo {}
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// could be
|
|
|
|
/// ```
|
|
|
|
/// struct Foo {}
|
|
|
|
/// impl Foo {
|
|
|
|
/// fn new() -> Self {
|
|
|
|
/// Self {}
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2017-07-28 06:28:07 -05:00
|
|
|
pub USE_SELF,
|
2018-03-28 08:24:26 -05:00
|
|
|
pedantic,
|
2017-08-18 12:42:26 -05:00
|
|
|
"Unnecessary structure name repetition whereas `Self` is applicable"
|
2017-07-28 06:28:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Default)]
|
|
|
|
pub struct UseSelf;
|
|
|
|
|
|
|
|
impl LintPass for UseSelf {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(USE_SELF)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 12:22:47 -05:00
|
|
|
const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
|
|
|
|
|
2017-07-28 06:28:07 -05:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
|
2017-08-21 05:58:06 -05:00
|
|
|
if in_macro(item.span) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-23 14:18:02 -05:00
|
|
|
if_chain! {
|
|
|
|
if let ItemImpl(.., ref item_type, ref refs) = item.node;
|
2018-07-12 03:03:06 -05:00
|
|
|
if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.node;
|
2017-10-23 14:18:02 -05:00
|
|
|
then {
|
2018-06-24 08:32:40 -05:00
|
|
|
let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
|
2017-10-23 14:18:02 -05:00
|
|
|
let should_check = if let Some(ref params) = *parameters {
|
2018-06-24 16:42:52 -05:00
|
|
|
!params.parenthesized && !params.args.iter().any(|arg| match arg {
|
|
|
|
GenericArg::Lifetime(_) => true,
|
|
|
|
GenericArg::Type(_) => false,
|
|
|
|
})
|
2017-10-23 14:18:02 -05:00
|
|
|
} else {
|
|
|
|
true
|
2017-08-25 02:30:21 -05:00
|
|
|
};
|
2017-10-23 14:18:02 -05:00
|
|
|
if should_check {
|
|
|
|
let visitor = &mut UseSelfVisitor {
|
2018-03-15 10:07:15 -05:00
|
|
|
item_path,
|
|
|
|
cx,
|
2017-10-23 14:18:02 -05:00
|
|
|
};
|
|
|
|
for impl_item_ref in refs {
|
|
|
|
visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
|
|
|
|
}
|
2017-08-25 02:30:21 -05:00
|
|
|
}
|
2017-07-28 06:28:07 -05:00
|
|
|
}
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
2017-07-28 06:28:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UseSelfVisitor<'a, 'tcx: 'a> {
|
|
|
|
item_path: &'a Path,
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
|
|
|
|
fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
|
2018-06-28 08:46:58 -05:00
|
|
|
if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfType.name() {
|
2017-08-18 12:42:26 -05:00
|
|
|
span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
|
2017-08-21 17:18:37 -05:00
|
|
|
db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
|
2017-08-18 12:42:26 -05:00
|
|
|
});
|
2017-07-28 06:28:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
walk_path(self, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
|
|
|
NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)
|
|
|
|
}
|
|
|
|
}
|