Skip to content

Rollup of 8 pull requests #59682

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

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1cfed0d
be more direct about borrow requirenments
matklad Apr 3, 2019
ab3b657
Updated the documentation of core::hints::spin_loop and core::sync::s…
Apr 3, 2019
becee90
Updated the reference in core::hint::spin_loop to the correct relativ…
Apr 3, 2019
7e37b46
Updated the environment description in rustc.
Apr 3, 2019
5b292ec
Revert rust-lld place changes.
o01eg Apr 3, 2019
c796b1f
Add tests for internal lints
flip1995 Dec 6, 2018
5c06567
Add internal lints default_hash_types and usage_of_ty_tykind
flip1995 Dec 6, 2018
4c9fb93
Uplift match_def_path from Clippy
flip1995 Dec 6, 2018
a2a8c44
Add register_internals function to `rustc_lint`
flip1995 Dec 6, 2018
157e797
Fix rebase fallout
flip1995 Feb 24, 2019
16acf7d
use check_path instead of check_expr
flip1995 Feb 28, 2019
9b2bf70
Make internal lints allow-by-default
flip1995 Mar 16, 2019
5a788f0
Fix bug in TyKind lint
flip1995 Mar 19, 2019
e536037
Deduplicate code in TyKind lint
flip1995 Mar 21, 2019
28a5c41
Check for unstable-options flag before register internals
flip1995 Mar 21, 2019
2045dfe
Update tests
flip1995 Mar 21, 2019
dfcd1ef
Add unstable-options flag to stage!=0
flip1995 Mar 31, 2019
69f74df
Deny internal lints in librustc
flip1995 Mar 31, 2019
d3f0cb9
Deny internal lints on non conflicting crates
flip1995 Mar 31, 2019
818d300
Deny internal lints on librustc_interface
flip1995 Mar 31, 2019
d2bc991
Deny internal lints on librustc_lint
flip1995 Mar 31, 2019
e4b87f5
Deny internal lints on librustc_mir
flip1995 Mar 31, 2019
4d2a3bb
Deny internal lints on librustc_typeck
flip1995 Mar 31, 2019
dd7483c
Remove TyKind arg from report_bin_hex_error function
flip1995 Apr 2, 2019
51a792d
Add trait_object_dummy_self to CommonTypes
flip1995 Apr 3, 2019
076abfa
Deny internal lints on two more crates
flip1995 Apr 3, 2019
c81ce06
Compare `Ty`s directly instead of their `TyKind`s
flip1995 Apr 3, 2019
4ef32a4
std: Avoid usage of `Once` in `Instant`
alexcrichton Apr 3, 2019
da99f46
rustfix coverage: Skip UI tests with non-json error-format
phansch Apr 3, 2019
fba110c
reduce repetition in librustc(_lint) wrt. impl LintPass
Centril Apr 3, 2019
714f282
Rollup merge of #59316 - flip1995:internal_lints_take_2, r=oli-obk
Centril Apr 3, 2019
b3fa795
Rollup merge of #59663 - matklad:borrow, r=dtolnay
Centril Apr 3, 2019
8fbba80
Rollup merge of #59664 - DevQps:improve-yield-spinlock-docs, r=alexcr…
Centril Apr 3, 2019
e598853
Rollup merge of #59666 - DevQps:update-rustc-environment-descriptions…
Centril Apr 3, 2019
79e1dd9
Rollup merge of #59669 - Centril:lint-pass-macro, r=oli-obk
Centril Apr 3, 2019
fa2dfda
Rollup merge of #59672 - o01eg:fix-59661, r=oli-obk
Centril Apr 3, 2019
ae5038f
Rollup merge of #59676 - alexcrichton:osx-deadlock, r=sfackler
Centril Apr 3, 2019
f5f8547
Rollup merge of #59677 - phansch:rustfix_coverage_handle_other_error_…
Centril Apr 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add internal lints default_hash_types and usage_of_ty_tykind
  • Loading branch information
flip1995 committed Apr 3, 2019
commit 5c0656789dfde752ea7af001e3d04a2a916685cf
165 changes: 165 additions & 0 deletions src/librustc/lint/internal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright 2012-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// 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.

//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
//! Clippy.

use errors::Applicability;
use hir::{Expr, ExprKind, PatKind, Path, QPath, Ty, TyKind};
use lint::{
EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass,
};
use rustc_data_structures::fx::FxHashMap;
use syntax::ast::Ident;

declare_lint! {
pub DEFAULT_HASH_TYPES,
Warn,
"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)
}
}

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_with_applicability(
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,
Warn,
"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)
}
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage {
fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &'tcx Expr) {
let qpaths = match &expr.node {
ExprKind::Match(_, arms, _) => {
let mut qpaths = vec![];
for arm in arms {
for pat in &arm.pats {
match &pat.node {
PatKind::Path(qpath) | PatKind::TupleStruct(qpath, ..) => {
qpaths.push(qpath)
}
_ => (),
}
}
}
qpaths
}
ExprKind::Path(qpath) => vec![qpath],
_ => vec![],
};
for qpath in qpaths {
if let QPath::Resolved(_, path) = qpath {
let segments_iter = path.segments.iter().rev().skip(1).rev();

if let Some(last) = segments_iter.clone().last() {
if last.ident.as_str() == "TyKind" {
let path = Path {
span: path.span.with_hi(last.ident.span.hi()),
def: path.def,
segments: segments_iter.cloned().collect(),
};

if let Some(def) = last.def {
if def
.def_id()
.match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"])
{
cx.struct_span_lint(
USAGE_OF_TY_TYKIND,
path.span,
"usage of `ty::TyKind::<kind>`",
)
.span_suggestion_with_applicability(
path.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 last.ident.as_str() == "TyKind" {
if let Some(def) = last.def {
if def
.def_id()
.match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"])
{
cx.struct_span_lint(
USAGE_OF_TY_TYKIND,
path.span,
"usage of `ty::TyKind`",
)
.help("try using `ty::Ty` instead")
.emit();
}
}
}
}
}
}
}
}
1 change: 1 addition & 0 deletions src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ impl_stable_hash_for!(enum self::LintSource {
pub type LevelSource = (Level, LintSource);

pub mod builtin;
pub mod internal;
mod context;
mod levels;

Expand Down