2018-10-06 16:18:06 +00:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-05-29 00:17:55 +00:00
|
|
|
//! lint on inherent implementations
|
|
|
|
|
2018-09-15 07:21:58 +00:00
|
|
|
use crate::rustc::hir::*;
|
|
|
|
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use crate::rustc::{declare_tool_lint, lint_array};
|
|
|
|
use crate::rustc_data_structures::fx::FxHashMap;
|
|
|
|
use crate::syntax_pos::Span;
|
2018-08-28 11:13:42 +00:00
|
|
|
use crate::utils::span_lint_and_then;
|
2018-11-27 20:14:15 +00:00
|
|
|
use std::default::Default;
|
2018-05-29 00:17:55 +00:00
|
|
|
|
|
|
|
/// **What it does:** Checks for multiple inherent implementations of a struct
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Splitting the implementation of a type makes the code harder to navigate.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// struct X;
|
|
|
|
/// impl X {
|
|
|
|
/// fn one() {}
|
|
|
|
/// }
|
|
|
|
/// impl X {
|
|
|
|
/// fn other() {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct X;
|
|
|
|
/// impl X {
|
|
|
|
/// fn one() {}
|
|
|
|
/// fn other() {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
declare_clippy_lint! {
|
|
|
|
pub MULTIPLE_INHERENT_IMPL,
|
2018-05-29 08:19:16 +00:00
|
|
|
restriction,
|
2018-05-29 00:17:55 +00:00
|
|
|
"Multiple inherent impl that could be grouped"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Pass {
|
2018-09-11 23:34:52 +00:00
|
|
|
impls: FxHashMap<def_id::DefId, (Span, Generics)>,
|
2018-05-29 00:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Pass {
|
|
|
|
fn default() -> Self {
|
2018-11-27 20:14:15 +00:00
|
|
|
Self {
|
|
|
|
impls: FxHashMap::default(),
|
|
|
|
}
|
2018-05-29 00:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for Pass {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(MULTIPLE_INHERENT_IMPL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
|
|
|
fn check_item(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
|
2018-07-16 13:07:39 +00:00
|
|
|
if let ItemKind::Impl(_, _, _, ref generics, None, _, _) = item.node {
|
2018-05-29 00:17:55 +00:00
|
|
|
// Remember for each inherent implementation encoutered its span and generics
|
|
|
|
self.impls
|
|
|
|
.insert(item.hir_id.owner_def_id(), (item.span, generics.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_crate_post(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx Crate) {
|
|
|
|
if let Some(item) = krate.items.values().nth(0) {
|
|
|
|
// Retrieve all inherent implementations from the crate, grouped by type
|
|
|
|
for impls in cx
|
|
|
|
.tcx
|
|
|
|
.crate_inherent_impls(item.hir_id.owner_def_id().krate)
|
|
|
|
.inherent_impls
|
|
|
|
.values()
|
|
|
|
{
|
|
|
|
// Filter out implementations that have generic params (type or lifetime)
|
|
|
|
let mut impl_spans = impls
|
|
|
|
.iter()
|
|
|
|
.filter_map(|impl_def| self.impls.get(impl_def))
|
2018-11-27 20:14:15 +00:00
|
|
|
.filter_map(|(span, generics)| if generics.params.len() == 0 { Some(span) } else { None });
|
2018-05-29 00:17:55 +00:00
|
|
|
if let Some(initial_span) = impl_spans.nth(0) {
|
|
|
|
impl_spans.for_each(|additional_span| {
|
2018-08-28 11:13:42 +00:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
2018-05-29 00:17:55 +00:00
|
|
|
MULTIPLE_INHERENT_IMPL,
|
|
|
|
*additional_span,
|
|
|
|
"Multiple implementations of this structure",
|
2018-08-28 11:13:42 +00:00
|
|
|
|db| {
|
2018-11-27 20:14:15 +00:00
|
|
|
db.span_note(*initial_span, "First implementation here");
|
2018-08-28 11:13:42 +00:00
|
|
|
},
|
2018-05-29 00:17:55 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|