Auto merge of #3966 - flip1995:internal_lints, r=oli-obk

Enable rustc internal lints

Closes #3965

I'm not 100% sure if enabling the `-Zunstable-options` flag in the `.cargo/config` file is the right place.
This commit is contained in:
bors 2019-04-17 04:46:19 +00:00
commit 27d62cf603
9 changed files with 10 additions and 126 deletions

View file

@ -1,2 +1,5 @@
[alias] [alias]
uitest = "test --test compile-test" uitest = "test --test compile-test"
[build]
rustflags = ["-Zunstable-options"]

View file

@ -1 +0,0 @@
stable

View file

@ -1,5 +1,3 @@
#![allow(clippy::default_hash_types)]
use itertools::Itertools; use itertools::Itertools;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;

View file

@ -8,6 +8,7 @@
#![allow(clippy::missing_docs_in_private_items)] #![allow(clippy::missing_docs_in_private_items)]
#![recursion_limit = "256"] #![recursion_limit = "256"]
#![warn(rust_2018_idioms, trivial_casts, trivial_numeric_casts)] #![warn(rust_2018_idioms, trivial_casts, trivial_numeric_casts)]
#![deny(internal)]
#![feature(crate_visibility_modifier)] #![feature(crate_visibility_modifier)]
// FIXME: switch to something more ergonomic here, once available. // FIXME: switch to something more ergonomic here, once available.
@ -423,7 +424,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
reg.register_late_lint_pass(box serde_api::Serde); reg.register_late_lint_pass(box serde_api::Serde);
reg.register_early_lint_pass(box utils::internal_lints::Clippy); reg.register_early_lint_pass(box utils::internal_lints::Clippy);
reg.register_late_lint_pass(box utils::internal_lints::CompilerLintFunctions::new()); reg.register_late_lint_pass(box utils::internal_lints::CompilerLintFunctions::new());
reg.register_early_lint_pass(box utils::internal_lints::DefaultHashTypes::default());
reg.register_late_lint_pass(box utils::internal_lints::LintWithoutLintPass::default()); reg.register_late_lint_pass(box utils::internal_lints::LintWithoutLintPass::default());
reg.register_late_lint_pass(box utils::inspector::Pass); reg.register_late_lint_pass(box utils::inspector::Pass);
reg.register_late_lint_pass(box utils::author::Pass); reg.register_late_lint_pass(box utils::author::Pass);
@ -647,7 +647,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
reg.register_lint_group("clippy::internal", Some("clippy_internal"), vec![ reg.register_lint_group("clippy::internal", Some("clippy_internal"), vec![
utils::internal_lints::CLIPPY_LINTS_INTERNAL, utils::internal_lints::CLIPPY_LINTS_INTERNAL,
utils::internal_lints::COMPILER_LINT_FUNCTIONS, utils::internal_lints::COMPILER_LINT_FUNCTIONS,
utils::internal_lints::DEFAULT_HASH_TYPES,
utils::internal_lints::LINT_WITHOUT_LINT_PASS, utils::internal_lints::LINT_WITHOUT_LINT_PASS,
]); ]);

View file

@ -9,7 +9,7 @@ use if_chain::if_chain;
use rustc::hir::def::CtorKind; use rustc::hir::def::CtorKind;
use rustc::hir::*; use rustc::hir::*;
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass}; use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
use rustc::ty::{self, Ty, TyKind}; use rustc::ty::{self, Ty};
use rustc::{declare_tool_lint, lint_array}; use rustc::{declare_tool_lint, lint_array};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use std::cmp::Ordering; use std::cmp::Ordering;
@ -500,7 +500,7 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
// already covered. // already covered.
let mut missing_variants = vec![]; let mut missing_variants = vec![];
if let TyKind::Adt(def, _) = ty.sty { if let ty::Adt(def, _) = ty.sty {
for variant in &def.variants { for variant in &def.variants {
missing_variants.push(variant); missing_variants.push(variant);
} }

View file

@ -1,4 +1,4 @@
#![allow(clippy::default_hash_types)] #![allow(default_hash_types)]
use std::borrow::Cow; use std::borrow::Cow;
use std::cmp::Ordering; use std::cmp::Ordering;

View file

@ -1,6 +1,4 @@
use crate::utils::{ use crate::utils::{match_def_path, match_type, paths, span_help_and_lint, span_lint, walk_ptrs_ty};
match_def_path, match_type, paths, span_help_and_lint, span_lint, span_lint_and_sugg, walk_ptrs_ty,
};
use if_chain::if_chain; use if_chain::if_chain;
use rustc::hir; use rustc::hir;
use rustc::hir::def::Def; use rustc::hir::def::Def;
@ -9,8 +7,7 @@ use rustc::hir::*;
use rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintPass}; use rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_tool_lint, lint_array}; use rustc::{declare_tool_lint, lint_array};
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::Applicability; use syntax::ast::{Crate as AstCrate, ItemKind, Name};
use syntax::ast::{Crate as AstCrate, Ident, ItemKind, Name};
use syntax::source_map::Span; use syntax::source_map::Span;
use syntax::symbol::LocalInternedString; use syntax::symbol::LocalInternedString;
@ -56,17 +53,6 @@ declare_clippy_lint! {
"declaring a lint without associating it in a LintPass" "declaring a lint without associating it in a LintPass"
} }
declare_clippy_lint! {
/// **What it does:** Checks for the presence of the default hash types "HashMap" or "HashSet"
/// and recommends the FxHash* variants.
///
/// **Why is this bad?** The FxHash variants have better performance
/// and we don't need any collision prevention in clippy.
pub DEFAULT_HASH_TYPES,
internal,
"forbid HashMap and HashSet and suggest the FxHash* variants"
}
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** Checks for calls to `cx.span_lint*` and suggests to use the `utils::*` /// **What it does:** Checks for calls to `cx.span_lint*` and suggests to use the `utils::*`
/// variant of the function. /// variant of the function.
@ -238,51 +224,6 @@ impl<'a, 'tcx: 'a> Visitor<'tcx> for LintCollector<'a, 'tcx> {
} }
} }
pub struct DefaultHashTypes {
map: FxHashMap<String, String>,
}
impl DefaultHashTypes {
pub fn default() -> Self {
let mut map = FxHashMap::default();
map.insert("HashMap".to_string(), "FxHashMap".to_string());
map.insert("HashSet".to_string(), "FxHashSet".to_string());
Self { map }
}
}
impl LintPass for DefaultHashTypes {
fn get_lints(&self) -> LintArray {
lint_array!(DEFAULT_HASH_TYPES)
}
fn name(&self) -> &'static str {
"DefaultHashType"
}
}
impl EarlyLintPass for DefaultHashTypes {
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
let ident_string = ident.to_string();
if let Some(replace) = self.map.get(&ident_string) {
let msg = format!(
"Prefer {} over {}, it has better performance \
and we don't need any collision prevention in clippy",
replace, ident_string
);
span_lint_and_sugg(
cx,
DEFAULT_HASH_TYPES,
ident.span,
&msg,
"use",
replace.to_string(),
Applicability::MaybeIncorrect, // FxHashMap, ... needs another import
);
}
}
}
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct CompilerLintFunctions { pub struct CompilerLintFunctions {
map: FxHashMap<String, String>, map: FxHashMap<String, String>,
@ -325,7 +266,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CompilerLintFunctions {
COMPILER_LINT_FUNCTIONS, COMPILER_LINT_FUNCTIONS,
path.ident.span, path.ident.span,
"usage of a compiler lint function", "usage of a compiler lint function",
&format!("Please use the Clippy variant of this function: `{}`", sugg), &format!("please use the Clippy variant of this function: `{}`", sugg),
); );
} }
} }

View file

@ -1,16 +0,0 @@
#![warn(clippy::default_hash_types)]
#![feature(rustc_private)]
extern crate rustc_data_structures;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use std::collections::{HashMap, HashSet};
fn main() {
let _map: HashMap<String, String> = HashMap::default();
let _set: HashSet<String> = HashSet::default();
// test that the lint doesn't also match the Fx variants themselves 😂
let _fx_map: FxHashMap<String, String> = FxHashMap::default();
let _fx_set: FxHashSet<String> = FxHashSet::default();
}

View file

@ -1,40 +0,0 @@
error: Prefer FxHashMap over HashMap, it has better performance and we don't need any collision prevention in clippy
--> $DIR/fxhash.rs:7:24
|
LL | use std::collections::{HashMap, HashSet};
| ^^^^^^^ help: use: `FxHashMap`
|
= note: `-D clippy::default-hash-types` implied by `-D warnings`
error: Prefer FxHashSet over HashSet, it has better performance and we don't need any collision prevention in clippy
--> $DIR/fxhash.rs:7:33
|
LL | use std::collections::{HashMap, HashSet};
| ^^^^^^^ help: use: `FxHashSet`
error: Prefer FxHashMap over HashMap, it has better performance and we don't need any collision prevention in clippy
--> $DIR/fxhash.rs:10:15
|
LL | let _map: HashMap<String, String> = HashMap::default();
| ^^^^^^^ help: use: `FxHashMap`
error: Prefer FxHashMap over HashMap, it has better performance and we don't need any collision prevention in clippy
--> $DIR/fxhash.rs:10:41
|
LL | let _map: HashMap<String, String> = HashMap::default();
| ^^^^^^^ help: use: `FxHashMap`
error: Prefer FxHashSet over HashSet, it has better performance and we don't need any collision prevention in clippy
--> $DIR/fxhash.rs:11:15
|
LL | let _set: HashSet<String> = HashSet::default();
| ^^^^^^^ help: use: `FxHashSet`
error: Prefer FxHashSet over HashSet, it has better performance and we don't need any collision prevention in clippy
--> $DIR/fxhash.rs:11:33
|
LL | let _set: HashSet<String> = HashSet::default();
| ^^^^^^^ help: use: `FxHashSet`
error: aborting due to 6 previous errors