forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#59316 - flip1995:internal_lints_take_2, r=o…
…li-obk Internal lints take 2 cc rust-lang#58701 cc rust-lang#49509 TODO: Add `#![warn(internal)]` to crates (and fix violations) Crates depending on `rustc_data_structures` - [x] librustc_resolve - [x] librustc_driver - [x] librustc_passes - [x] librustc_metadata - [x] librustc_interface - [x] librustc_save_analysis - [x] librustc_lint - [x] librustc - [x] librustc_incremental - [x] librustc_codegen_utils - [x] libarena - [x] librustc_target - [x] librustc_allocator - [x] librustc_privacy - [x] librustc_traits - [x] librustc_borrowck - [x] libsyntax - [x] librustc_codegen_ssa - [x] libsyntax_ext - [x] librustc_errors - [x] librustc_mir - [x] libsyntax_pos - [x] librustc_typeck Crates with `feature(rustc_private)` Excluding crates, which are already in the list above. Also excluding tools and tests. - [ ] ~~libstd~~ - [x] libfmt_macros - [x] librustdoc r? @oli-obk
- Loading branch information
Showing
58 changed files
with
684 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as | ||
//! Clippy. | ||
use crate::hir::{HirId, Path, PathSegment, QPath, Ty, TyKind}; | ||
use crate::lint::{ | ||
EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass, | ||
}; | ||
use errors::Applicability; | ||
use rustc_data_structures::fx::FxHashMap; | ||
use syntax::ast::Ident; | ||
|
||
declare_lint! { | ||
pub DEFAULT_HASH_TYPES, | ||
Allow, | ||
"forbid HashMap and HashSet and suggest the FxHash* variants" | ||
} | ||
|
||
pub struct DefaultHashTypes { | ||
map: FxHashMap<String, String>, | ||
} | ||
|
||
impl DefaultHashTypes { | ||
pub fn new() -> 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 { | ||
"DefaultHashTypes" | ||
} | ||
} | ||
|
||
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", | ||
replace, ident_string | ||
); | ||
let mut db = cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, &msg); | ||
db.span_suggestion( | ||
ident.span, | ||
"use", | ||
replace.to_string(), | ||
Applicability::MaybeIncorrect, // FxHashMap, ... needs another import | ||
); | ||
db.note(&format!( | ||
"a `use rustc_data_structures::fx::{}` may be necessary", | ||
replace | ||
)) | ||
.emit(); | ||
} | ||
} | ||
} | ||
|
||
declare_lint! { | ||
pub USAGE_OF_TY_TYKIND, | ||
Allow, | ||
"Usage of `ty::TyKind` outside of the `ty::sty` module" | ||
} | ||
|
||
pub struct TyKindUsage; | ||
|
||
impl LintPass for TyKindUsage { | ||
fn get_lints(&self) -> LintArray { | ||
lint_array!(USAGE_OF_TY_TYKIND) | ||
} | ||
|
||
fn name(&self) -> &'static str { | ||
"TyKindUsage" | ||
} | ||
} | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage { | ||
fn check_path(&mut self, cx: &LateContext<'_, '_>, path: &'tcx Path, _: HirId) { | ||
let segments = path.segments.iter().rev().skip(1).rev(); | ||
|
||
if let Some(last) = segments.last() { | ||
let span = path.span.with_hi(last.ident.span.hi()); | ||
if lint_ty_kind_usage(cx, last) { | ||
cx.struct_span_lint(USAGE_OF_TY_TYKIND, span, "usage of `ty::TyKind::<kind>`") | ||
.span_suggestion( | ||
span, | ||
"try using ty::<kind> directly", | ||
"ty".to_string(), | ||
Applicability::MaybeIncorrect, // ty maybe needs an import | ||
) | ||
.emit(); | ||
} | ||
} | ||
} | ||
|
||
fn check_ty(&mut self, cx: &LateContext<'_, '_>, ty: &'tcx Ty) { | ||
if let TyKind::Path(qpath) = &ty.node { | ||
if let QPath::Resolved(_, path) = qpath { | ||
if let Some(last) = path.segments.iter().last() { | ||
if lint_ty_kind_usage(cx, last) { | ||
cx.struct_span_lint(USAGE_OF_TY_TYKIND, path.span, "usage of `ty::TyKind`") | ||
.help("try using `ty::Ty` instead") | ||
.emit(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment) -> bool { | ||
if segment.ident.as_str() == "TyKind" { | ||
if let Some(def) = segment.def { | ||
if let Some(did) = def.opt_def_id() { | ||
return did.match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]); | ||
} | ||
} | ||
} | ||
|
||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.