Skip to content

Commit 995d34e

Browse files
authored
Rollup merge of rust-lang#61545 - flip1995:internal_lints, r=oli-obk
Implement another internal lints cc rust-lang#49509 This adds ~~two~~ one internal lint~~s~~: 1. LINT_PASS_IMPL_WITHOUT_MACRO: Make sure, that the `{declare,impl}_lint_pass` macro is used to implement lint passes. cc rust-lang#59669 2. ~~USAGE_OF_TYCTXT_AND_SPAN_ARGS: item 2 on the list in rust-lang#49509~~ ~~With 2. I wasn't sure, if this lint should be applied everywhere. That means a careful review of 0955835 would be great. Also 73fb9b4 allows this lint on some functions. Should I also apply this lint there?~~ TODO (not directly relevant for review): - [ ] rust-lang#59316 (comment) (not sure yet, if this works or how to query for `rustc_private`, since it's not in [`Features`](https://doc.rust-lang.org/nightly/nightly-rustc/syntax/feature_gate/struct.Features.html) 🤔 cc @eddyb) - [x] rust-lang#61735 (comment) - [x] Check explicitly for the `{declare,impl}_lint_pass!` macros r? @oli-obk
2 parents 29a035f + d0625a3 commit 995d34e

File tree

49 files changed

+209
-89
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+209
-89
lines changed

src/bootstrap/bin/rustc.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,20 @@ fn main() {
306306
}
307307

308308
// This is required for internal lints.
309-
cmd.arg("-Zunstable-options");
309+
if let Some(crate_name) = args.windows(2).find(|a| &*a[0] == "--crate-name") {
310+
let crate_name = crate_name[1].to_string_lossy();
311+
if crate_name != "rustc_version"
312+
&& (crate_name.starts_with("rustc")
313+
|| crate_name.starts_with("syntax")
314+
|| crate_name == "arena"
315+
|| crate_name == "fmt_macros")
316+
{
317+
cmd.arg("-Zunstable-options");
318+
if stage != "0" {
319+
cmd.arg("-Wrustc::internal");
320+
}
321+
}
322+
}
310323

311324
// Force all crates compiled by this compiler to (a) be unstable and (b)
312325
// allow the `rustc_private` feature to link to other unstable crates

src/libarena/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
test(no_crate_inject, attr(deny(warnings))))]
1313

1414
#![deny(rust_2018_idioms)]
15-
#![deny(internal)]
1615
#![deny(unused_lifetimes)]
1716

1817
#![feature(core_intrinsics)]

src/libfmt_macros/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
test(attr(deny(warnings))))]
1010

1111
#![deny(rust_2018_idioms)]
12-
#![deny(internal)]
1312
#![deny(unused_lifetimes)]
1413

1514
#![feature(nll)]

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
3030

3131
#![deny(rust_2018_idioms)]
32-
#![deny(internal)]
3332
#![deny(unused_lifetimes)]
3433

3534
#![feature(arbitrary_self_types)]

src/librustc/lint/context.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,7 @@ struct LateLintPassObjects<'a> {
13411341
lints: &'a mut [LateLintPassObject],
13421342
}
13431343

1344+
#[cfg_attr(not(bootstrap), allow(rustc::lint_pass_impl_without_macro))]
13441345
impl LintPass for LateLintPassObjects<'_> {
13451346
fn name(&self) -> &'static str {
13461347
panic!()
@@ -1510,6 +1511,7 @@ struct EarlyLintPassObjects<'a> {
15101511
lints: &'a mut [EarlyLintPassObject],
15111512
}
15121513

1514+
#[cfg_attr(not(bootstrap), allow(rustc::lint_pass_impl_without_macro))]
15131515
impl LintPass for EarlyLintPassObjects<'_> {
15141516
fn name(&self) -> &'static str {
15151517
panic!()

src/librustc/lint/internal.rs

+56-26
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ use crate::lint::{
77
};
88
use errors::Applicability;
99
use rustc_data_structures::fx::FxHashMap;
10-
use syntax::ast::Ident;
10+
use syntax::ast::{Ident, Item, ItemKind};
1111
use syntax::symbol::{sym, Symbol};
12+
use syntax_pos::ExpnInfo;
1213

13-
declare_lint! {
14-
pub DEFAULT_HASH_TYPES,
14+
declare_tool_lint! {
15+
pub rustc::DEFAULT_HASH_TYPES,
1516
Allow,
1617
"forbid HashMap and HashSet and suggest the FxHash* variants"
1718
}
@@ -22,7 +23,7 @@ pub struct DefaultHashTypes {
2223

2324
impl DefaultHashTypes {
2425
// we are allowed to use `HashMap` and `HashSet` as identifiers for implementing the lint itself
25-
#[allow(internal)]
26+
#[cfg_attr(not(bootstrap), allow(rustc::default_hash_types))]
2627
pub fn new() -> Self {
2728
let mut map = FxHashMap::default();
2829
map.insert(sym::HashMap, sym::FxHashMap);
@@ -36,40 +37,34 @@ impl_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
3637
impl EarlyLintPass for DefaultHashTypes {
3738
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
3839
if let Some(replace) = self.map.get(&ident.name) {
39-
let msg = format!(
40-
"Prefer {} over {}, it has better performance",
41-
replace, ident
42-
);
40+
let msg = format!("Prefer {} over {}, it has better performance", replace, ident);
4341
let mut db = cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, &msg);
4442
db.span_suggestion(
4543
ident.span,
4644
"use",
4745
replace.to_string(),
4846
Applicability::MaybeIncorrect, // FxHashMap, ... needs another import
4947
);
50-
db.note(&format!(
51-
"a `use rustc_data_structures::fx::{}` may be necessary",
52-
replace
53-
))
54-
.emit();
48+
db.note(&format!("a `use rustc_data_structures::fx::{}` may be necessary", replace))
49+
.emit();
5550
}
5651
}
5752
}
5853

59-
declare_lint! {
60-
pub USAGE_OF_TY_TYKIND,
54+
declare_tool_lint! {
55+
pub rustc::USAGE_OF_TY_TYKIND,
6156
Allow,
6257
"usage of `ty::TyKind` outside of the `ty::sty` module"
6358
}
6459

65-
declare_lint! {
66-
pub TY_PASS_BY_REFERENCE,
60+
declare_tool_lint! {
61+
pub rustc::TY_PASS_BY_REFERENCE,
6762
Allow,
6863
"passing `Ty` or `TyCtxt` by reference"
6964
}
7065

71-
declare_lint! {
72-
pub USAGE_OF_QUALIFIED_TY,
66+
declare_tool_lint! {
67+
pub rustc::USAGE_OF_QUALIFIED_TY,
7368
Allow,
7469
"using `ty::{Ty,TyCtxt}` instead of importing it"
7570
}
@@ -137,13 +132,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
137132
}
138133
}
139134
}
140-
TyKind::Rptr(
141-
_,
142-
MutTy {
143-
ty: inner_ty,
144-
mutbl: Mutability::MutImmutable,
145-
},
146-
) => {
135+
TyKind::Rptr(_, MutTy { ty: inner_ty, mutbl: Mutability::MutImmutable }) => {
147136
if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner_def_id()) {
148137
if cx.tcx.impl_trait_ref(impl_did).is_some() {
149138
return;
@@ -225,3 +214,44 @@ fn gen_args(segment: &PathSegment) -> String {
225214

226215
String::new()
227216
}
217+
218+
declare_tool_lint! {
219+
pub rustc::LINT_PASS_IMPL_WITHOUT_MACRO,
220+
Allow,
221+
"`impl LintPass` without the `declare_lint_pass!` or `impl_lint_pass!` macros"
222+
}
223+
224+
declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
225+
226+
impl EarlyLintPass for LintPassImpl {
227+
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
228+
if let ItemKind::Impl(_, _, _, _, Some(lint_pass), _, _) = &item.node {
229+
if let Some(last) = lint_pass.path.segments.last() {
230+
if last.ident.name == sym::LintPass {
231+
match &lint_pass.path.span.ctxt().outer_expn_info() {
232+
Some(info) if is_lint_pass_expansion(info) => {}
233+
_ => {
234+
cx.struct_span_lint(
235+
LINT_PASS_IMPL_WITHOUT_MACRO,
236+
lint_pass.path.span,
237+
"implementing `LintPass` by hand",
238+
)
239+
.help("try using `declare_lint_pass!` or `impl_lint_pass!` instead")
240+
.emit();
241+
}
242+
}
243+
}
244+
}
245+
}
246+
}
247+
}
248+
249+
fn is_lint_pass_expansion(expn_info: &ExpnInfo) -> bool {
250+
if expn_info.format.name() == sym::impl_lint_pass {
251+
true
252+
} else if let Some(info) = expn_info.call_site.ctxt().outer_expn_info() {
253+
info.format.name() == sym::declare_lint_pass
254+
} else {
255+
false
256+
}
257+
}

src/librustc/ty/codec.rs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub trait EncodableWithShorthand: Clone + Eq + Hash {
2727
fn variant(&self) -> &Self::Variant;
2828
}
2929

30+
#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))]
3031
impl<'tcx> EncodableWithShorthand for Ty<'tcx> {
3132
type Variant = ty::TyKind<'tcx>;
3233
fn variant(&self) -> &Self::Variant {
@@ -159,6 +160,7 @@ where
159160
Ok(decoder.map_encoded_cnum_to_current(cnum))
160161
}
161162

163+
#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))]
162164
#[inline]
163165
pub fn decode_ty<D>(decoder: &mut D) -> Result<Ty<'tcx>, D::Error>
164166
where

src/librustc/ty/context.rs

+3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ impl<'tcx> CtxtInterners<'tcx> {
130130
}
131131

132132
/// Intern a type
133+
#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))]
133134
#[inline(never)]
134135
fn intern_ty(&self,
135136
st: TyKind<'tcx>
@@ -2107,6 +2108,7 @@ impl<'tcx> Hash for Interned<'tcx, TyS<'tcx>> {
21072108
}
21082109
}
21092110

2111+
#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))]
21102112
impl<'tcx> Borrow<TyKind<'tcx>> for Interned<'tcx, TyS<'tcx>> {
21112113
fn borrow<'a>(&'a self) -> &'a TyKind<'tcx> {
21122114
&self.0.sty
@@ -2321,6 +2323,7 @@ impl<'tcx> TyCtxt<'tcx> {
23212323
self.mk_fn_ptr(converted_sig)
23222324
}
23232325

2326+
#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))]
23242327
#[inline]
23252328
pub fn mk_ty(&self, st: TyKind<'tcx>) -> Ty<'tcx> {
23262329
self.interners.intern_ty(st)

src/librustc/ty/flags.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl FlagComputation {
1818
}
1919
}
2020

21+
#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))]
2122
pub fn for_sty(st: &ty::TyKind<'_>) -> FlagComputation {
2223
let mut result = FlagComputation::new();
2324
result.add_sty(st);
@@ -61,6 +62,7 @@ impl FlagComputation {
6162
} // otherwise, this binder captures nothing
6263
}
6364

65+
#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))]
6466
fn add_sty(&mut self, st: &ty::TyKind<'_>) {
6567
match st {
6668
&ty::Bool |

src/librustc/ty/mod.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// ignore-tidy-filelength
22

3-
#![allow(usage_of_ty_tykind)]
4-
53
pub use self::Variance::*;
64
pub use self::AssocItemContainer::*;
75
pub use self::BorrowKind::*;
@@ -484,6 +482,7 @@ bitflags! {
484482
}
485483
}
486484

485+
#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))]
487486
pub struct TyS<'tcx> {
488487
pub sty: TyKind<'tcx>,
489488
pub flags: TypeFlags,
@@ -541,29 +540,29 @@ impl<'tcx> Hash for TyS<'tcx> {
541540
impl<'tcx> TyS<'tcx> {
542541
pub fn is_primitive_ty(&self) -> bool {
543542
match self.sty {
544-
TyKind::Bool |
545-
TyKind::Char |
546-
TyKind::Int(_) |
547-
TyKind::Uint(_) |
548-
TyKind::Float(_) |
549-
TyKind::Infer(InferTy::IntVar(_)) |
550-
TyKind::Infer(InferTy::FloatVar(_)) |
551-
TyKind::Infer(InferTy::FreshIntTy(_)) |
552-
TyKind::Infer(InferTy::FreshFloatTy(_)) => true,
553-
TyKind::Ref(_, x, _) => x.is_primitive_ty(),
543+
Bool |
544+
Char |
545+
Int(_) |
546+
Uint(_) |
547+
Float(_) |
548+
Infer(InferTy::IntVar(_)) |
549+
Infer(InferTy::FloatVar(_)) |
550+
Infer(InferTy::FreshIntTy(_)) |
551+
Infer(InferTy::FreshFloatTy(_)) => true,
552+
Ref(_, x, _) => x.is_primitive_ty(),
554553
_ => false,
555554
}
556555
}
557556

558557
pub fn is_suggestable(&self) -> bool {
559558
match self.sty {
560-
TyKind::Opaque(..) |
561-
TyKind::FnDef(..) |
562-
TyKind::FnPtr(..) |
563-
TyKind::Dynamic(..) |
564-
TyKind::Closure(..) |
565-
TyKind::Infer(..) |
566-
TyKind::Projection(..) => false,
559+
Opaque(..) |
560+
FnDef(..) |
561+
FnPtr(..) |
562+
Dynamic(..) |
563+
Closure(..) |
564+
Infer(..) |
565+
Projection(..) => false,
567566
_ => true,
568567
}
569568
}

src/librustc/ty/sty.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! This module contains `TyKind` and its major components.
22
3+
#![cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))]
4+
35
use crate::hir;
46
use crate::hir::def_id::DefId;
57
use crate::infer::canonical::Canonical;

src/librustc_allocator/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#![feature(rustc_private)]
33

44
#![deny(rust_2018_idioms)]
5-
#![deny(internal)]
65
#![deny(unused_lifetimes)]
76

87
pub mod expand;

src/librustc_borrowck/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#![allow(non_camel_case_types)]
44
#![deny(rust_2018_idioms)]
5-
#![deny(internal)]
65
#![deny(unused_lifetimes)]
76

87
#![feature(in_band_lifetimes)]

src/librustc_codegen_llvm/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#![feature(trusted_len)]
2424
#![feature(mem_take)]
2525
#![deny(rust_2018_idioms)]
26-
#![deny(internal)]
2726
#![deny(unused_lifetimes)]
2827

2928
use back::write::{create_target_machine, create_informational_target_machine};

src/librustc_codegen_ssa/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#![allow(unused_attributes)]
1515
#![allow(dead_code)]
1616
#![deny(rust_2018_idioms)]
17-
#![deny(internal)]
1817
#![deny(unused_lifetimes)]
1918

2019
#![recursion_limit="256"]

src/librustc_codegen_utils/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#![recursion_limit="256"]
1818

1919
#![deny(rust_2018_idioms)]
20-
#![deny(internal)]
2120
#![deny(unused_lifetimes)]
2221

2322
#[macro_use]

src/librustc_data_structures/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#![cfg_attr(test, feature(test))]
2828

2929
#![deny(rust_2018_idioms)]
30+
#![cfg_attr(not(bootstrap), allow(rustc::default_hash_types))]
3031

3132
#[macro_use]
3233
extern crate log;

src/librustc_driver/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#![recursion_limit="256"]
1818

1919
#![deny(rust_2018_idioms)]
20-
#![deny(internal)]
2120
#![deny(unused_lifetimes)]
2221

2322
pub extern crate getopts;

src/librustc_errors/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#![feature(nll)]
1111
#![feature(optin_builtin_traits)]
1212
#![deny(rust_2018_idioms)]
13-
#![deny(internal)]
1413
#![deny(unused_lifetimes)]
1514

1615
#[allow(unused_extern_crates)]

src/librustc_incremental/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#![recursion_limit="256"]
1010

1111
#![deny(rust_2018_idioms)]
12-
#![deny(internal)]
1312
#![deny(unused_lifetimes)]
1413

1514
#[macro_use] extern crate rustc;

0 commit comments

Comments
 (0)