2017-07-28 11:28:07 +00:00
|
|
|
use rustc::lint::{LintArray, LateLintPass, LateContext, LintPass};
|
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::hir::intravisit::{Visitor, walk_path, NestedVisitorMap};
|
2017-08-21 10:58:06 +00:00
|
|
|
use utils::{span_lint_and_then, in_macro};
|
2017-07-28 11:28:07 +00:00
|
|
|
use syntax::ast::NodeId;
|
2017-08-18 17:42:26 +00:00
|
|
|
use syntax_pos::symbol::keywords::SelfType;
|
2017-07-28 11:28:07 +00:00
|
|
|
|
|
|
|
/// **What it does:** Checks for unnecessary repetition of structure name when a
|
|
|
|
/// replacement with `Self` is applicable.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct name
|
|
|
|
/// 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 {}
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
declare_lint! {
|
|
|
|
pub USE_SELF,
|
|
|
|
Allow,
|
2017-08-18 17:42:26 +00:00
|
|
|
"Unnecessary structure name repetition whereas `Self` is applicable"
|
2017-07-28 11:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Default)]
|
|
|
|
pub struct UseSelf;
|
|
|
|
|
|
|
|
impl LintPass for UseSelf {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(USE_SELF)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:22:47 +00:00
|
|
|
const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
|
|
|
|
|
2017-07-28 11:28:07 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
|
2017-08-21 10:58:06 +00:00
|
|
|
if in_macro(item.span) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-28 11:28:07 +00:00
|
|
|
if_let_chain!([
|
|
|
|
let ItemImpl(.., ref item_type, ref refs) = item.node,
|
|
|
|
let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node,
|
|
|
|
], {
|
2017-08-25 07:30:21 +00:00
|
|
|
let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).parameters;
|
|
|
|
if !parameters.parenthesized && parameters.lifetimes.len() == 0 {
|
|
|
|
let visitor = &mut UseSelfVisitor {
|
|
|
|
item_path: item_path,
|
|
|
|
cx: cx,
|
|
|
|
};
|
|
|
|
for impl_item_ref in refs {
|
|
|
|
visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
|
|
|
|
}
|
2017-07-28 11:28:07 +00: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) {
|
|
|
|
if self.item_path.def == path.def &&
|
|
|
|
path.segments
|
|
|
|
.last()
|
2017-08-22 17:22:47 +00:00
|
|
|
.expect(SEGMENTS_MSG)
|
2017-08-18 17:42:26 +00:00
|
|
|
.name != SelfType.name() {
|
|
|
|
span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
|
2017-08-21 22:18:37 +00:00
|
|
|
db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
|
2017-08-18 17:42:26 +00:00
|
|
|
});
|
2017-07-28 11:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
walk_path(self, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
|
|
|
NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)
|
|
|
|
}
|
|
|
|
}
|