1 //! Lints, aka compiler warnings.
3 //! A 'lint' check is a kind of miscellaneous constraint that a user _might_
4 //! want to enforce, but might reasonably want to permit as well, on a
5 //! module-by-module basis. They contrast with static constraints enforced by
6 //! other phases of the compiler, which are generally required to hold in order
7 //! to compile the program at all.
9 //! Most lints can be written as [LintPass] instances. These run after
10 //! all other analyses. The `LintPass`es built into rustc are defined
11 //! within [rustc_session::lint::builtin],
12 //! which has further comments on how to add such a lint.
13 //! rustc can also load user-defined lint plugins via the plugin mechanism.
15 //! Some of rustc's lints are defined elsewhere in the compiler and work by
16 //! calling `add_lint()` on the overall `Session` object. This works when
17 //! it happens before the main lint pass, which emits the lints stored by
18 //! `add_lint()`. To emit lints after the main lint pass (from codegen, for
19 //! example) requires more effort. See `emit_lint` and `GatherNodeLevels`
22 //! Some code also exists in [rustc_session::lint], [rustc_middle::lint].
26 //! This API is completely unstable and subject to change.
28 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
29 #![cfg_attr(test, feature(test))]
30 #![feature(array_windows)]
31 #![feature(bool_to_option)]
32 #![feature(box_syntax)]
33 #![feature(crate_visibility_modifier)]
34 #![feature(iter_order_by)]
35 #![feature(never_type)]
37 #![feature(or_patterns)]
38 #![feature(half_open_range_patterns)]
39 #![feature(exclusive_range_pattern)]
40 #![recursion_limit = "256"]
43 extern crate rustc_middle;
45 extern crate rustc_session;
56 mod nonstandard_style;
58 mod redundant_semicolon;
65 use rustc_hir::def_id::LocalDefId;
66 use rustc_middle::ty::query::Providers;
67 use rustc_middle::ty::TyCtxt;
68 use rustc_session::lint::builtin::{
69 BARE_TRAIT_OBJECTS, BROKEN_INTRA_DOC_LINKS, ELIDED_LIFETIMES_IN_PATHS,
70 EXPLICIT_OUTLIVES_REQUIREMENTS, INVALID_CODEBLOCK_ATTRIBUTES, INVALID_HTML_TAGS,
71 MISSING_DOC_CODE_EXAMPLES, PRIVATE_DOC_TESTS,
73 use rustc_span::symbol::{Ident, Symbol};
76 use array_into_iter::ArrayIntoIter;
80 use non_ascii_idents::*;
81 use nonstandard_style::*;
82 use redundant_semicolon::*;
87 /// Useful for other parts of the compiler / Clippy.
88 pub use builtin::SoftLints;
89 pub use context::{CheckLintNameResult, EarlyContext, LateContext, LintContext, LintStore};
90 pub use early::check_ast_crate;
91 pub use late::check_crate;
92 pub use passes::{EarlyLintPass, LateLintPass};
93 pub use rustc_session::lint::Level::{self, *};
94 pub use rustc_session::lint::{BufferedEarlyLint, FutureIncompatibleInfo, Lint, LintId};
95 pub use rustc_session::lint::{LintArray, LintPass};
97 pub fn provide(providers: &mut Providers) {
98 levels::provide(providers);
99 *providers = Providers { lint_mod, ..*providers };
102 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
103 late::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
106 macro_rules! pre_expansion_lint_passes {
107 ($macro:path, $args:tt) => {
108 $macro!($args, [KeywordIdents: KeywordIdents,]);
112 macro_rules! early_lint_passes {
113 ($macro:path, $args:tt) => {
117 UnusedParens: UnusedParens,
118 UnusedBraces: UnusedBraces,
119 UnusedImportBraces: UnusedImportBraces,
120 UnsafeCode: UnsafeCode,
121 AnonymousParameters: AnonymousParameters,
122 EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
123 NonCamelCaseTypes: NonCamelCaseTypes,
124 DeprecatedAttr: DeprecatedAttr::new(),
125 WhileTrue: WhileTrue,
126 NonAsciiIdents: NonAsciiIdents,
127 IncompleteFeatures: IncompleteFeatures,
128 RedundantSemicolons: RedundantSemicolons,
129 UnusedDocComment: UnusedDocComment,
135 macro_rules! declare_combined_early_pass {
136 ([$name:ident], $passes:tt) => (
137 early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
141 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
142 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
144 macro_rules! late_lint_passes {
145 ($macro:path, $args:tt) => {
149 // FIXME: Look into regression when this is used as a module lint
150 // May Depend on constants elsewhere
151 UnusedBrokenConst: UnusedBrokenConst,
152 // Uses attr::is_used which is untracked, can't be an incremental module pass.
153 UnusedAttributes: UnusedAttributes::new(),
154 // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
155 UnstableFeatures: UnstableFeatures,
156 // Tracks state across modules
157 UnnameableTestItems: UnnameableTestItems::new(),
158 // Tracks attributes of parents
159 MissingDoc: MissingDoc::new(),
160 // Depends on access levels
161 // FIXME: Turn the computation of types which implement Debug into a query
162 // and change this to a module lint pass
163 MissingDebugImplementations: MissingDebugImplementations::default(),
164 ArrayIntoIter: ArrayIntoIter,
165 ClashingExternDeclarations: ClashingExternDeclarations::new(),
166 DropTraitConstraints: DropTraitConstraints,
167 TemporaryCStringAsPtr: TemporaryCStringAsPtr,
173 macro_rules! late_lint_mod_passes {
174 ($macro:path, $args:tt) => {
178 HardwiredLints: HardwiredLints,
179 ImproperCTypesDeclarations: ImproperCTypesDeclarations,
180 ImproperCTypesDefinitions: ImproperCTypesDefinitions,
181 VariantSizeDifferences: VariantSizeDifferences,
182 BoxPointers: BoxPointers,
183 PathStatements: PathStatements,
184 // Depends on referenced function signatures in expressions
185 UnusedResults: UnusedResults,
186 NonUpperCaseGlobals: NonUpperCaseGlobals,
187 NonShorthandFieldPatterns: NonShorthandFieldPatterns,
188 UnusedAllocation: UnusedAllocation,
189 // Depends on types used in type definitions
190 MissingCopyImplementations: MissingCopyImplementations,
191 // Depends on referenced function signatures in expressions
192 MutableTransmutes: MutableTransmutes,
193 TypeAliasBounds: TypeAliasBounds,
194 TrivialConstraints: TrivialConstraints,
195 TypeLimits: TypeLimits::new(),
196 NonSnakeCase: NonSnakeCase,
197 InvalidNoMangleItems: InvalidNoMangleItems,
198 // Depends on access levels
199 UnreachablePub: UnreachablePub,
200 ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
201 InvalidValue: InvalidValue,
207 macro_rules! declare_combined_late_pass {
208 ([$v:vis $name:ident], $passes:tt) => (
209 late_lint_methods!(declare_combined_late_lint_pass, [$v $name, $passes], ['tcx]);
213 // FIXME: Make a separate lint type which do not require typeck tables
214 late_lint_passes!(declare_combined_late_pass, [pub BuiltinCombinedLateLintPass]);
216 late_lint_mod_passes!(declare_combined_late_pass, [BuiltinCombinedModuleLateLintPass]);
218 pub fn new_lint_store(no_interleave_lints: bool, internal_lints: bool) -> LintStore {
219 let mut lint_store = LintStore::new();
221 register_builtins(&mut lint_store, no_interleave_lints);
223 register_internals(&mut lint_store);
229 /// Tell the `LintStore` about all the built-in lints (the ones
230 /// defined in this crate and the ones defined in
231 /// `rustc_session::lint::builtin`).
232 fn register_builtins(store: &mut LintStore, no_interleave_lints: bool) {
233 macro_rules! add_lint_group {
234 ($name:expr, $($lint:ident),*) => (
235 store.register_group(false, $name, None, vec![$(LintId::of($lint)),*]);
239 macro_rules! register_pass {
240 ($method:ident, $ty:ident, $constructor:expr) => {
241 store.register_lints(&$ty::get_lints());
242 store.$method(|| box $constructor);
246 macro_rules! register_passes {
247 ($method:ident, [$($passes:ident: $constructor:expr,)*]) => (
249 register_pass!($method, $passes, $constructor);
254 if no_interleave_lints {
255 pre_expansion_lint_passes!(register_passes, register_pre_expansion_pass);
256 early_lint_passes!(register_passes, register_early_pass);
257 late_lint_passes!(register_passes, register_late_pass);
258 late_lint_mod_passes!(register_passes, register_late_mod_pass);
260 store.register_lints(&BuiltinCombinedPreExpansionLintPass::get_lints());
261 store.register_lints(&BuiltinCombinedEarlyLintPass::get_lints());
262 store.register_lints(&BuiltinCombinedModuleLateLintPass::get_lints());
263 store.register_lints(&BuiltinCombinedLateLintPass::get_lints());
268 NON_CAMEL_CASE_TYPES,
270 NON_UPPER_CASE_GLOBALS
281 UNREACHABLE_PATTERNS,
282 OVERLAPPING_PATTERNS,
290 UNUSED_EXTERN_CRATES,
301 UNUSED_EXTERN_CRATES,
302 ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
303 ELIDED_LIFETIMES_IN_PATHS,
304 EXPLICIT_OUTLIVES_REQUIREMENTS // FIXME(#52665, #47816) not always applicable and not all
305 // macros are ready for this yet.
308 // FIXME macro crates are not up for this yet, too much
309 // breakage is seen if we try to encourage this lint.
310 // MACRO_USE_EXTERN_CRATE
315 BROKEN_INTRA_DOC_LINKS,
316 PRIVATE_INTRA_DOC_LINKS,
317 INVALID_CODEBLOCK_ATTRIBUTES,
318 MISSING_DOC_CODE_EXAMPLES,
323 // Register renamed and removed lints.
324 store.register_renamed("single_use_lifetime", "single_use_lifetimes");
325 store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
326 store.register_renamed("bare_trait_object", "bare_trait_objects");
327 store.register_renamed("unstable_name_collision", "unstable_name_collisions");
328 store.register_renamed("unused_doc_comment", "unused_doc_comments");
329 store.register_renamed("async_idents", "keyword_idents");
330 store.register_renamed("exceeding_bitshifts", "arithmetic_overflow");
331 store.register_renamed("redundant_semicolon", "redundant_semicolons");
332 store.register_renamed("intra_doc_link_resolution_failure", "broken_intra_doc_links");
333 store.register_removed("unknown_features", "replaced by an error");
334 store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
335 store.register_removed("negate_unsigned", "cast a signed value instead");
336 store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
337 // Register lint group aliases.
338 store.register_group_alias("nonstandard_style", "bad_style");
339 // This was renamed to `raw_pointer_derive`, which was then removed,
340 // so it is also considered removed.
341 store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
342 store.register_removed("drop_with_repr_extern", "drop flags have been removed");
343 store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
344 store.register_removed("deprecated_attr", "use `deprecated` instead");
345 store.register_removed(
346 "transmute_from_fn_item_types",
347 "always cast functions before transmuting them",
349 store.register_removed(
350 "hr_lifetime_in_assoc_type",
351 "converted into hard error, see issue #33685 \
352 <https://github.com/rust-lang/rust/issues/33685> for more information",
354 store.register_removed(
355 "inaccessible_extern_crate",
356 "converted into hard error, see issue #36886 \
357 <https://github.com/rust-lang/rust/issues/36886> for more information",
359 store.register_removed(
360 "super_or_self_in_global_path",
361 "converted into hard error, see issue #36888 \
362 <https://github.com/rust-lang/rust/issues/36888> for more information",
364 store.register_removed(
365 "overlapping_inherent_impls",
366 "converted into hard error, see issue #36889 \
367 <https://github.com/rust-lang/rust/issues/36889> for more information",
369 store.register_removed(
370 "illegal_floating_point_constant_pattern",
371 "converted into hard error, see issue #36890 \
372 <https://github.com/rust-lang/rust/issues/36890> for more information",
374 store.register_removed(
375 "illegal_struct_or_enum_constant_pattern",
376 "converted into hard error, see issue #36891 \
377 <https://github.com/rust-lang/rust/issues/36891> for more information",
379 store.register_removed(
380 "lifetime_underscore",
381 "converted into hard error, see issue #36892 \
382 <https://github.com/rust-lang/rust/issues/36892> for more information",
384 store.register_removed(
385 "extra_requirement_in_impl",
386 "converted into hard error, see issue #37166 \
387 <https://github.com/rust-lang/rust/issues/37166> for more information",
389 store.register_removed(
391 "converted into hard error, see issue #38260 \
392 <https://github.com/rust-lang/rust/issues/38260> for more information",
394 store.register_removed(
396 "converted into hard error, see issue #48950 \
397 <https://github.com/rust-lang/rust/issues/48950> for more information",
399 store.register_removed(
400 "resolve_trait_on_defaulted_unit",
401 "converted into hard error, see issue #48950 \
402 <https://github.com/rust-lang/rust/issues/48950> for more information",
404 store.register_removed(
405 "private_no_mangle_fns",
406 "no longer a warning, `#[no_mangle]` functions always exported",
408 store.register_removed(
409 "private_no_mangle_statics",
410 "no longer a warning, `#[no_mangle]` statics always exported",
412 store.register_removed("bad_repr", "replaced with a generic attribute input check");
413 store.register_removed(
414 "duplicate_matcher_binding_name",
415 "converted into hard error, see issue #57742 \
416 <https://github.com/rust-lang/rust/issues/57742> for more information",
418 store.register_removed(
419 "incoherent_fundamental_impls",
420 "converted into hard error, see issue #46205 \
421 <https://github.com/rust-lang/rust/issues/46205> for more information",
423 store.register_removed(
424 "legacy_constructor_visibility",
425 "converted into hard error, see issue #39207 \
426 <https://github.com/rust-lang/rust/issues/39207> for more information",
428 store.register_removed(
429 "legacy_directory_ownership",
430 "converted into hard error, see issue #37872 \
431 <https://github.com/rust-lang/rust/issues/37872> for more information",
433 store.register_removed(
434 "safe_extern_statics",
435 "converted into hard error, see issue #36247 \
436 <https://github.com/rust-lang/rust/issues/36247> for more information",
438 store.register_removed(
439 "parenthesized_params_in_types_and_modules",
440 "converted into hard error, see issue #42238 \
441 <https://github.com/rust-lang/rust/issues/42238> for more information",
443 store.register_removed(
444 "duplicate_macro_exports",
445 "converted into hard error, see issue #35896 \
446 <https://github.com/rust-lang/rust/issues/35896> for more information",
448 store.register_removed(
450 "converted into hard error, see issue #59014 \
451 <https://github.com/rust-lang/rust/issues/59014> for more information",
453 store.register_removed("plugin_as_library", "plugins have been deprecated and retired");
456 fn register_internals(store: &mut LintStore) {
457 store.register_lints(&DefaultHashTypes::get_lints());
458 store.register_early_pass(|| box DefaultHashTypes::new());
459 store.register_lints(&LintPassImpl::get_lints());
460 store.register_early_pass(|| box LintPassImpl);
461 store.register_lints(&TyTyKind::get_lints());
462 store.register_late_pass(|| box TyTyKind);
463 store.register_group(
468 LintId::of(DEFAULT_HASH_TYPES),
469 LintId::of(USAGE_OF_TY_TYKIND),
470 LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
471 LintId::of(TY_PASS_BY_REFERENCE),
472 LintId::of(USAGE_OF_QUALIFIED_TY),