1 //! # Lints in the Rust compiler
3 //! This currently only contains the definitions and implementations
4 //! of most of the lints that `rustc` supports directly, it does not
5 //! contain the infrastructure for defining/registering lints. That is
6 //! available in `rustc::lint` and `rustc_driver::plugin` respectively.
10 //! This API is completely unstable and subject to change.
12 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
14 #![cfg_attr(test, feature(test))]
15 #![feature(box_patterns)]
16 #![feature(box_syntax)]
19 #![recursion_limit="256"]
25 mod nonstandard_style;
26 mod redundant_semicolon;
33 use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
34 use rustc::lint::builtin::{
36 ELIDED_LIFETIMES_IN_PATHS,
37 EXPLICIT_OUTLIVES_REQUIREMENTS,
38 INTRA_DOC_LINK_RESOLUTION_FAILURE,
39 MISSING_DOC_CODE_EXAMPLES,
43 use rustc::hir::def_id::DefId;
44 use rustc::ty::query::Providers;
45 use rustc::ty::TyCtxt;
52 use redundant_semicolon::*;
53 use nonstandard_style::*;
57 use non_ascii_idents::*;
58 use rustc::lint::internal::*;
60 /// Useful for other parts of the compiler.
61 pub use builtin::SoftLints;
63 pub fn provide(providers: &mut Providers<'_>) {
64 *providers = Providers {
70 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
71 lint::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
74 macro_rules! pre_expansion_lint_passes {
75 ($macro:path, $args:tt) => (
77 KeywordIdents: KeywordIdents,
78 UnusedDocComment: UnusedDocComment,
83 macro_rules! early_lint_passes {
84 ($macro:path, $args:tt) => (
86 UnusedParens: UnusedParens,
87 UnusedImportBraces: UnusedImportBraces,
88 UnsafeCode: UnsafeCode,
89 AnonymousParameters: AnonymousParameters,
90 EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
91 NonCamelCaseTypes: NonCamelCaseTypes,
92 DeprecatedAttr: DeprecatedAttr::new(),
94 NonAsciiIdents: NonAsciiIdents,
95 IncompleteFeatures: IncompleteFeatures,
96 RedundantSemicolon: RedundantSemicolon,
101 macro_rules! declare_combined_early_pass {
102 ([$name:ident], $passes:tt) => (
103 early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
107 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
108 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
110 macro_rules! late_lint_passes {
111 ($macro:path, $args:tt) => (
113 // FIXME: Look into regression when this is used as a module lint
114 // May Depend on constants elsewhere
115 UnusedBrokenConst: UnusedBrokenConst,
117 // Uses attr::is_used which is untracked, can't be an incremental module pass.
118 UnusedAttributes: UnusedAttributes::new(),
120 // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
121 UnstableFeatures: UnstableFeatures,
123 // Tracks state across modules
124 UnnameableTestItems: UnnameableTestItems::new(),
126 // Tracks attributes of parents
127 MissingDoc: MissingDoc::new(),
129 // Depends on access levels
130 // FIXME: Turn the computation of types which implement Debug into a query
131 // and change this to a module lint pass
132 MissingDebugImplementations: MissingDebugImplementations::default(),
137 macro_rules! late_lint_mod_passes {
138 ($macro:path, $args:tt) => (
140 HardwiredLints: HardwiredLints,
141 ImproperCTypes: ImproperCTypes,
142 VariantSizeDifferences: VariantSizeDifferences,
143 BoxPointers: BoxPointers,
144 PathStatements: PathStatements,
146 // Depends on referenced function signatures in expressions
147 UnusedResults: UnusedResults,
149 NonUpperCaseGlobals: NonUpperCaseGlobals,
150 NonShorthandFieldPatterns: NonShorthandFieldPatterns,
151 UnusedAllocation: UnusedAllocation,
153 // Depends on types used in type definitions
154 MissingCopyImplementations: MissingCopyImplementations,
156 PluginAsLibrary: PluginAsLibrary,
158 // Depends on referenced function signatures in expressions
159 MutableTransmutes: MutableTransmutes,
161 TypeAliasBounds: TypeAliasBounds,
163 TrivialConstraints: TrivialConstraints,
164 TypeLimits: TypeLimits::new(),
166 NonSnakeCase: NonSnakeCase,
167 InvalidNoMangleItems: InvalidNoMangleItems,
169 // Depends on access levels
170 UnreachablePub: UnreachablePub,
172 ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
173 InvalidValue: InvalidValue,
178 macro_rules! declare_combined_late_pass {
179 ([$v:vis $name:ident], $passes:tt) => (
180 late_lint_methods!(declare_combined_late_lint_pass, [$v $name, $passes], ['tcx]);
184 // FIXME: Make a separate lint type which do not require typeck tables
185 late_lint_passes!(declare_combined_late_pass, [pub BuiltinCombinedLateLintPass]);
187 late_lint_mod_passes!(declare_combined_late_pass, [BuiltinCombinedModuleLateLintPass]);
189 pub fn new_lint_store(no_interleave_lints: bool, internal_lints: bool) -> lint::LintStore {
190 let mut lint_store = lint::LintStore::new();
192 register_builtins(&mut lint_store, no_interleave_lints);
194 register_internals(&mut lint_store);
200 /// Tell the `LintStore` about all the built-in lints (the ones
201 /// defined in this crate and the ones defined in
202 /// `rustc::lint::builtin`).
203 fn register_builtins(store: &mut lint::LintStore, no_interleave_lints: bool) {
204 macro_rules! add_lint_group {
205 ($name:expr, $($lint:ident),*) => (
206 store.register_group(false, $name, None, vec![$(LintId::of($lint)),*]);
210 macro_rules! register_pass {
211 ($method:ident, $ty:ident, $constructor:expr) => (
212 store.register_lints(&$ty::get_lints());
213 store.$method(|| box $constructor);
217 macro_rules! register_passes {
218 ($method:ident, [$($passes:ident: $constructor:expr,)*]) => (
220 register_pass!($method, $passes, $constructor);
225 if no_interleave_lints {
226 pre_expansion_lint_passes!(register_passes, register_pre_expansion_pass);
227 early_lint_passes!(register_passes, register_early_pass);
228 late_lint_passes!(register_passes, register_late_pass);
229 late_lint_mod_passes!(register_passes, register_late_mod_pass);
231 store.register_lints(&BuiltinCombinedPreExpansionLintPass::get_lints());
232 store.register_lints(&BuiltinCombinedEarlyLintPass::get_lints());
233 store.register_lints(&BuiltinCombinedModuleLateLintPass::get_lints());
234 store.register_lints(&BuiltinCombinedLateLintPass::get_lints());
237 add_lint_group!("nonstandard_style",
238 NON_CAMEL_CASE_TYPES,
240 NON_UPPER_CASE_GLOBALS);
242 add_lint_group!("unused",
249 UNREACHABLE_PATTERNS,
250 OVERLAPPING_PATTERNS,
258 UNUSED_EXTERN_CRATES,
263 add_lint_group!("rust_2018_idioms",
265 UNUSED_EXTERN_CRATES,
266 ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
267 ELIDED_LIFETIMES_IN_PATHS,
268 EXPLICIT_OUTLIVES_REQUIREMENTS
270 // FIXME(#52665, #47816) not always applicable and not all
271 // macros are ready for this yet.
274 // FIXME macro crates are not up for this yet, too much
275 // breakage is seen if we try to encourage this lint.
276 // MACRO_USE_EXTERN_CRATE,
279 add_lint_group!("rustdoc",
280 INTRA_DOC_LINK_RESOLUTION_FAILURE,
281 MISSING_DOC_CODE_EXAMPLES,
284 // Register renamed and removed lints.
285 store.register_renamed("single_use_lifetime", "single_use_lifetimes");
286 store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
287 store.register_renamed("bare_trait_object", "bare_trait_objects");
288 store.register_renamed("unstable_name_collision", "unstable_name_collisions");
289 store.register_renamed("unused_doc_comment", "unused_doc_comments");
290 store.register_renamed("async_idents", "keyword_idents");
291 store.register_removed("unknown_features", "replaced by an error");
292 store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
293 store.register_removed("negate_unsigned", "cast a signed value instead");
294 store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
295 // Register lint group aliases.
296 store.register_group_alias("nonstandard_style", "bad_style");
297 // This was renamed to `raw_pointer_derive`, which was then removed,
298 // so it is also considered removed.
299 store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
300 store.register_removed("drop_with_repr_extern", "drop flags have been removed");
301 store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
302 store.register_removed("deprecated_attr", "use `deprecated` instead");
303 store.register_removed("transmute_from_fn_item_types",
304 "always cast functions before transmuting them");
305 store.register_removed("hr_lifetime_in_assoc_type",
306 "converted into hard error, see https://github.com/rust-lang/rust/issues/33685");
307 store.register_removed("inaccessible_extern_crate",
308 "converted into hard error, see https://github.com/rust-lang/rust/issues/36886");
309 store.register_removed("super_or_self_in_global_path",
310 "converted into hard error, see https://github.com/rust-lang/rust/issues/36888");
311 store.register_removed("overlapping_inherent_impls",
312 "converted into hard error, see https://github.com/rust-lang/rust/issues/36889");
313 store.register_removed("illegal_floating_point_constant_pattern",
314 "converted into hard error, see https://github.com/rust-lang/rust/issues/36890");
315 store.register_removed("illegal_struct_or_enum_constant_pattern",
316 "converted into hard error, see https://github.com/rust-lang/rust/issues/36891");
317 store.register_removed("lifetime_underscore",
318 "converted into hard error, see https://github.com/rust-lang/rust/issues/36892");
319 store.register_removed("extra_requirement_in_impl",
320 "converted into hard error, see https://github.com/rust-lang/rust/issues/37166");
321 store.register_removed("legacy_imports",
322 "converted into hard error, see https://github.com/rust-lang/rust/issues/38260");
323 store.register_removed("coerce_never",
324 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
325 store.register_removed("resolve_trait_on_defaulted_unit",
326 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
327 store.register_removed("private_no_mangle_fns",
328 "no longer a warning, `#[no_mangle]` functions always exported");
329 store.register_removed("private_no_mangle_statics",
330 "no longer a warning, `#[no_mangle]` statics always exported");
331 store.register_removed("bad_repr",
332 "replaced with a generic attribute input check");
333 store.register_removed("duplicate_matcher_binding_name",
334 "converted into hard error, see https://github.com/rust-lang/rust/issues/57742");
335 store.register_removed("incoherent_fundamental_impls",
336 "converted into hard error, see https://github.com/rust-lang/rust/issues/46205");
337 store.register_removed("legacy_constructor_visibility",
338 "converted into hard error, see https://github.com/rust-lang/rust/issues/39207");
339 store.register_removed("legacy_disrectory_ownership",
340 "converted into hard error, see https://github.com/rust-lang/rust/issues/37872");
341 store.register_removed("safe_extern_statics",
342 "converted into hard error, see https://github.com/rust-lang/rust/issues/36247");
343 store.register_removed("parenthesized_params_in_types_and_modules",
344 "converted into hard error, see https://github.com/rust-lang/rust/issues/42238");
345 store.register_removed("duplicate_macro_exports",
346 "converted into hard error, see https://github.com/rust-lang/rust/issues/35896");
349 fn register_internals(store: &mut lint::LintStore) {
350 store.register_lints(&DefaultHashTypes::get_lints());
351 store.register_early_pass(|| box DefaultHashTypes::new());
352 store.register_lints(&LintPassImpl::get_lints());
353 store.register_early_pass(|| box LintPassImpl);
354 store.register_lints(&TyTyKind::get_lints());
355 store.register_late_pass(|| box TyTyKind);
356 store.register_group(
361 LintId::of(DEFAULT_HASH_TYPES),
362 LintId::of(USAGE_OF_TY_TYKIND),
363 LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
364 LintId::of(TY_PASS_BY_REFERENCE),
365 LintId::of(USAGE_OF_QUALIFIED_TY),