-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Internal lints take 2 #59316
Merged
Merged
Internal lints take 2 #59316
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c796b1f
Add tests for internal lints
flip1995 5c06567
Add internal lints default_hash_types and usage_of_ty_tykind
flip1995 4c9fb93
Uplift match_def_path from Clippy
flip1995 a2a8c44
Add register_internals function to `rustc_lint`
flip1995 157e797
Fix rebase fallout
flip1995 16acf7d
use check_path instead of check_expr
flip1995 9b2bf70
Make internal lints allow-by-default
flip1995 5a788f0
Fix bug in TyKind lint
flip1995 e536037
Deduplicate code in TyKind lint
flip1995 28a5c41
Check for unstable-options flag before register internals
flip1995 2045dfe
Update tests
flip1995 dfcd1ef
Add unstable-options flag to stage!=0
flip1995 69f74df
Deny internal lints in librustc
flip1995 d3f0cb9
Deny internal lints on non conflicting crates
flip1995 818d300
Deny internal lints on librustc_interface
flip1995 d2bc991
Deny internal lints on librustc_lint
flip1995 e4b87f5
Deny internal lints on librustc_mir
flip1995 4d2a3bb
Deny internal lints on librustc_typeck
flip1995 dd7483c
Remove TyKind arg from report_bin_hex_error function
flip1995 51a792d
Add trait_object_dummy_self to CommonTypes
flip1995 076abfa
Deny internal lints on two more crates
flip1995 c81ce06
Compare `Ty`s directly instead of their `TyKind`s
flip1995 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry for not noticing this until now, but such functionality doesn't belong on
DefId
, not evenTyCtxt
IMO.Could we move this to the lint context, instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will do this tomorrow/Saturday!