2018-10-06 09:18:06 -07: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.
|
|
|
|
|
|
|
|
|
2016-02-03 15:38:23 +01:00
|
|
|
//! lint on `use`ing all variants of an enum
|
|
|
|
|
2018-09-15 10:21:58 +03:00
|
|
|
use crate::rustc::hir::*;
|
|
|
|
use crate::rustc::hir::def::Def;
|
|
|
|
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use crate::rustc::{declare_tool_lint, lint_array};
|
|
|
|
use crate::syntax::ast::NodeId;
|
|
|
|
use crate::syntax::source_map::Span;
|
2018-05-30 10:15:50 +02:00
|
|
|
use crate::utils::span_lint;
|
2016-02-03 15:38:23 +01:00
|
|
|
|
2016-08-06 09:55:04 +02:00
|
|
|
/// **What it does:** Checks for `use Enum::*`.
|
2016-02-03 15:38:23 +01:00
|
|
|
///
|
2016-08-06 09:55:04 +02:00
|
|
|
/// **Why is this bad?** It is usually better style to use the prefixed name of
|
|
|
|
/// an enumeration variant, rather than importing variants.
|
2016-02-03 15:38:23 +01:00
|
|
|
///
|
2016-08-06 09:55:04 +02:00
|
|
|
/// **Known problems:** Old-style enumerations that prefix the variants are
|
2018-03-26 07:48:32 +02:00
|
|
|
/// still around.
|
2016-02-03 15:38:23 +01:00
|
|
|
///
|
2016-07-16 00:25:44 +02:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// use std::cmp::Ordering::*;
|
|
|
|
/// ```
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2016-08-06 10:18:36 +02:00
|
|
|
pub ENUM_GLOB_USE,
|
2018-03-28 15:24:26 +02:00
|
|
|
pedantic,
|
2016-08-06 10:18:36 +02:00
|
|
|
"use items that import all variants of an enum"
|
|
|
|
}
|
2016-02-03 15:38:23 +01:00
|
|
|
|
|
|
|
pub struct EnumGlobUse;
|
|
|
|
|
|
|
|
impl LintPass for EnumGlobUse {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(ENUM_GLOB_USE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:13:40 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
|
|
|
|
fn check_mod(&mut self, cx: &LateContext<'a, 'tcx>, m: &'tcx Mod, _: Span, _: NodeId) {
|
2016-02-03 15:38:23 +01:00
|
|
|
// only check top level `use` statements
|
|
|
|
for item in &m.item_ids {
|
2017-06-11 05:34:47 +03:00
|
|
|
self.lint_item(cx, cx.tcx.hir.expect_item(item.id));
|
2016-02-03 15:38:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumGlobUse {
|
2018-07-23 13:01:12 +02:00
|
|
|
fn lint_item(&self, cx: &LateContext<'_, '_>, item: &Item) {
|
2018-07-15 00:00:27 +02:00
|
|
|
if item.vis.node.is_pub() {
|
2016-02-03 15:38:23 +01:00
|
|
|
return; // re-exports are fine
|
|
|
|
}
|
2018-07-16 15:07:39 +02:00
|
|
|
if let ItemKind::Use(ref path, UseKind::Glob) = item.node {
|
2018-03-26 07:48:32 +02:00
|
|
|
if let Def::Enum(_) = path.def {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
ENUM_GLOB_USE,
|
|
|
|
item.span,
|
|
|
|
"don't use glob imports for enum variants",
|
|
|
|
);
|
2016-02-03 15:38:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|