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;
55 mod nonstandard_style;
57 mod redundant_semicolon;
64 use rustc_hir::def_id::LocalDefId;
65 use rustc_middle::ty::query::Providers;
66 use rustc_middle::ty::TyCtxt;
67 use rustc_session::lint::builtin::{
68 BARE_TRAIT_OBJECTS, BROKEN_INTRA_DOC_LINKS, ELIDED_LIFETIMES_IN_PATHS,
69 EXPLICIT_OUTLIVES_REQUIREMENTS, INVALID_CODEBLOCK_ATTRIBUTES, INVALID_HTML_TAGS,
70 MISSING_DOC_CODE_EXAMPLES, PRIVATE_DOC_TESTS,
72 use rustc_span::symbol::{Ident, Symbol};
75 use array_into_iter::ArrayIntoIter;
78 use non_ascii_idents::*;
79 use nonstandard_style::*;
80 use redundant_semicolon::*;
85 /// Useful for other parts of the compiler / Clippy.
86 pub use builtin::SoftLints;
87 pub use context::{CheckLintNameResult, EarlyContext, LateContext, LintContext, LintStore};
88 pub use early::check_ast_crate;
89 pub use late::check_crate;
90 pub use passes::{EarlyLintPass, LateLintPass};
91 pub use rustc_session::lint::Level::{self, *};
92 pub use rustc_session::lint::{BufferedEarlyLint, FutureIncompatibleInfo, Lint, LintId};
93 pub use rustc_session::lint::{LintArray, LintPass};
95 pub fn provide(providers: &mut Providers) {
96 levels::provide(providers);
97 *providers = Providers { lint_mod, ..*providers };
100 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
101 late::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
104 macro_rules! pre_expansion_lint_passes {
105 ($macro:path, $args:tt) => {
106 $macro!($args, [KeywordIdents: KeywordIdents,]);
110 macro_rules! early_lint_passes {
111 ($macro:path, $args:tt) => {
115 UnusedParens: UnusedParens,
116 UnusedBraces: UnusedBraces,
117 UnusedImportBraces: UnusedImportBraces,
118 UnsafeCode: UnsafeCode,
119 AnonymousParameters: AnonymousParameters,
120 EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
121 NonCamelCaseTypes: NonCamelCaseTypes,
122 DeprecatedAttr: DeprecatedAttr::new(),
123 WhileTrue: WhileTrue,
124 NonAsciiIdents: NonAsciiIdents,
125 IncompleteFeatures: IncompleteFeatures,
126 RedundantSemicolons: RedundantSemicolons,
127 UnusedDocComment: UnusedDocComment,
133 macro_rules! declare_combined_early_pass {
134 ([$name:ident], $passes:tt) => (
135 early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
139 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
140 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
142 macro_rules! late_lint_passes {
143 ($macro:path, $args:tt) => {
147 // FIXME: Look into regression when this is used as a module lint
148 // May Depend on constants elsewhere
149 UnusedBrokenConst: UnusedBrokenConst,
150 // Uses attr::is_used which is untracked, can't be an incremental module pass.
151 UnusedAttributes: UnusedAttributes::new(),
152 // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
153 UnstableFeatures: UnstableFeatures,
154 // Tracks state across modules
155 UnnameableTestItems: UnnameableTestItems::new(),
156 // Tracks attributes of parents
157 MissingDoc: MissingDoc::new(),
158 // Depends on access levels
159 // FIXME: Turn the computation of types which implement Debug into a query
160 // and change this to a module lint pass
161 MissingDebugImplementations: MissingDebugImplementations::default(),
162 ArrayIntoIter: ArrayIntoIter,
163 ClashingExternDeclarations: ClashingExternDeclarations::new(),
164 DropTraitConstraints: DropTraitConstraints,
170 macro_rules! late_lint_mod_passes {
171 ($macro:path, $args:tt) => {
175 HardwiredLints: HardwiredLints,
176 ImproperCTypesDeclarations: ImproperCTypesDeclarations,
177 ImproperCTypesDefinitions: ImproperCTypesDefinitions,
178 VariantSizeDifferences: VariantSizeDifferences,
179 BoxPointers: BoxPointers,
180 PathStatements: PathStatements,
181 // Depends on referenced function signatures in expressions
182 UnusedResults: UnusedResults,
183 NonUpperCaseGlobals: NonUpperCaseGlobals,
184 NonShorthandFieldPatterns: NonShorthandFieldPatterns,
185 UnusedAllocation: UnusedAllocation,
186 // Depends on types used in type definitions
187 MissingCopyImplementations: MissingCopyImplementations,
188 // Depends on referenced function signatures in expressions
189 MutableTransmutes: MutableTransmutes,
190 TypeAliasBounds: TypeAliasBounds,
191 TrivialConstraints: TrivialConstraints,
192 TypeLimits: TypeLimits::new(),
193 NonSnakeCase: NonSnakeCase,
194 InvalidNoMangleItems: InvalidNoMangleItems,
195 // Depends on access levels
196 UnreachablePub: UnreachablePub,
197 ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
198 InvalidValue: InvalidValue,
204 macro_rules! declare_combined_late_pass {
205 ([$v:vis $name:ident], $passes:tt) => (
206 late_lint_methods!(declare_combined_late_lint_pass, [$v $name, $passes], ['tcx]);
210 // FIXME: Make a separate lint type which do not require typeck tables
211 late_lint_passes!(declare_combined_late_pass, [pub BuiltinCombinedLateLintPass]);
213 late_lint_mod_passes!(declare_combined_late_pass, [BuiltinCombinedModuleLateLintPass]);
215 pub fn new_lint_store(no_interleave_lints: bool, internal_lints: bool) -> LintStore {
216 let mut lint_store = LintStore::new();
218 register_builtins(&mut lint_store, no_interleave_lints);
220 register_internals(&mut lint_store);
226 /// Tell the `LintStore` about all the built-in lints (the ones
227 /// defined in this crate and the ones defined in
228 /// `rustc_session::lint::builtin`).
229 fn register_builtins(store: &mut LintStore, no_interleave_lints: bool) {
230 macro_rules! add_lint_group {
231 ($name:expr, $($lint:ident),*) => (
232 store.register_group(false, $name, None, vec![$(LintId::of($lint)),*]);
236 macro_rules! register_pass {
237 ($method:ident, $ty:ident, $constructor:expr) => {
238 store.register_lints(&$ty::get_lints());
239 store.$method(|| box $constructor);
243 macro_rules! register_passes {
244 ($method:ident, [$($passes:ident: $constructor:expr,)*]) => (
246 register_pass!($method, $passes, $constructor);
251 if no_interleave_lints {
252 pre_expansion_lint_passes!(register_passes, register_pre_expansion_pass);
253 early_lint_passes!(register_passes, register_early_pass);
254 late_lint_passes!(register_passes, register_late_pass);
255 late_lint_mod_passes!(register_passes, register_late_mod_pass);
257 store.register_lints(&BuiltinCombinedPreExpansionLintPass::get_lints());
258 store.register_lints(&BuiltinCombinedEarlyLintPass::get_lints());
259 store.register_lints(&BuiltinCombinedModuleLateLintPass::get_lints());
260 store.register_lints(&BuiltinCombinedLateLintPass::get_lints());
265 NON_CAMEL_CASE_TYPES,
267 NON_UPPER_CASE_GLOBALS
278 UNREACHABLE_PATTERNS,
279 OVERLAPPING_PATTERNS,
287 UNUSED_EXTERN_CRATES,
298 UNUSED_EXTERN_CRATES,
299 ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
300 ELIDED_LIFETIMES_IN_PATHS,
301 EXPLICIT_OUTLIVES_REQUIREMENTS // FIXME(#52665, #47816) not always applicable and not all
302 // macros are ready for this yet.
305 // FIXME macro crates are not up for this yet, too much
306 // breakage is seen if we try to encourage this lint.
307 // MACRO_USE_EXTERN_CRATE
312 BROKEN_INTRA_DOC_LINKS,
313 PRIVATE_INTRA_DOC_LINKS,
314 INVALID_CODEBLOCK_ATTRIBUTES,
315 MISSING_DOC_CODE_EXAMPLES,
320 // Register renamed and removed lints.
321 store.register_renamed("single_use_lifetime", "single_use_lifetimes");
322 store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
323 store.register_renamed("bare_trait_object", "bare_trait_objects");
324 store.register_renamed("unstable_name_collision", "unstable_name_collisions");
325 store.register_renamed("unused_doc_comment", "unused_doc_comments");
326 store.register_renamed("async_idents", "keyword_idents");
327 store.register_renamed("exceeding_bitshifts", "arithmetic_overflow");
328 store.register_renamed("redundant_semicolon", "redundant_semicolons");
329 store.register_renamed("intra_doc_link_resolution_failure", "broken_intra_doc_links");
330 store.register_removed("unknown_features", "replaced by an error");
331 store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
332 store.register_removed("negate_unsigned", "cast a signed value instead");
333 store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
334 // Register lint group aliases.
335 store.register_group_alias("nonstandard_style", "bad_style");
336 // This was renamed to `raw_pointer_derive`, which was then removed,
337 // so it is also considered removed.
338 store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
339 store.register_removed("drop_with_repr_extern", "drop flags have been removed");
340 store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
341 store.register_removed("deprecated_attr", "use `deprecated` instead");
342 store.register_removed(
343 "transmute_from_fn_item_types",
344 "always cast functions before transmuting them",
346 store.register_removed(
347 "hr_lifetime_in_assoc_type",
348 "converted into hard error, see issue #33685 \
349 <https://github.com/rust-lang/rust/issues/33685> for more information",
351 store.register_removed(
352 "inaccessible_extern_crate",
353 "converted into hard error, see issue #36886 \
354 <https://github.com/rust-lang/rust/issues/36886> for more information",
356 store.register_removed(
357 "super_or_self_in_global_path",
358 "converted into hard error, see issue #36888 \
359 <https://github.com/rust-lang/rust/issues/36888> for more information",
361 store.register_removed(
362 "overlapping_inherent_impls",
363 "converted into hard error, see issue #36889 \
364 <https://github.com/rust-lang/rust/issues/36889> for more information",
366 store.register_removed(
367 "illegal_floating_point_constant_pattern",
368 "converted into hard error, see issue #36890 \
369 <https://github.com/rust-lang/rust/issues/36890> for more information",
371 store.register_removed(
372 "illegal_struct_or_enum_constant_pattern",
373 "converted into hard error, see issue #36891 \
374 <https://github.com/rust-lang/rust/issues/36891> for more information",
376 store.register_removed(
377 "lifetime_underscore",
378 "converted into hard error, see issue #36892 \
379 <https://github.com/rust-lang/rust/issues/36892> for more information",
381 store.register_removed(
382 "extra_requirement_in_impl",
383 "converted into hard error, see issue #37166 \
384 <https://github.com/rust-lang/rust/issues/37166> for more information",
386 store.register_removed(
388 "converted into hard error, see issue #38260 \
389 <https://github.com/rust-lang/rust/issues/38260> for more information",
391 store.register_removed(
393 "converted into hard error, see issue #48950 \
394 <https://github.com/rust-lang/rust/issues/48950> for more information",
396 store.register_removed(
397 "resolve_trait_on_defaulted_unit",
398 "converted into hard error, see issue #48950 \
399 <https://github.com/rust-lang/rust/issues/48950> for more information",
401 store.register_removed(
402 "private_no_mangle_fns",
403 "no longer a warning, `#[no_mangle]` functions always exported",
405 store.register_removed(
406 "private_no_mangle_statics",
407 "no longer a warning, `#[no_mangle]` statics always exported",
409 store.register_removed("bad_repr", "replaced with a generic attribute input check");
410 store.register_removed(
411 "duplicate_matcher_binding_name",
412 "converted into hard error, see issue #57742 \
413 <https://github.com/rust-lang/rust/issues/57742> for more information",
415 store.register_removed(
416 "incoherent_fundamental_impls",
417 "converted into hard error, see issue #46205 \
418 <https://github.com/rust-lang/rust/issues/46205> for more information",
420 store.register_removed(
421 "legacy_constructor_visibility",
422 "converted into hard error, see issue #39207 \
423 <https://github.com/rust-lang/rust/issues/39207> for more information",
425 store.register_removed(
426 "legacy_directory_ownership",
427 "converted into hard error, see issue #37872 \
428 <https://github.com/rust-lang/rust/issues/37872> for more information",
430 store.register_removed(
431 "safe_extern_statics",
432 "converted into hard error, see issue #36247 \
433 <https://github.com/rust-lang/rust/issues/36247> for more information",
435 store.register_removed(
436 "parenthesized_params_in_types_and_modules",
437 "converted into hard error, see issue #42238 \
438 <https://github.com/rust-lang/rust/issues/42238> for more information",
440 store.register_removed(
441 "duplicate_macro_exports",
442 "converted into hard error, see issue #35896 \
443 <https://github.com/rust-lang/rust/issues/35896> for more information",
445 store.register_removed(
447 "converted into hard error, see issue #59014 \
448 <https://github.com/rust-lang/rust/issues/59014> for more information",
450 store.register_removed("plugin_as_library", "plugins have been deprecated and retired");
453 fn register_internals(store: &mut LintStore) {
454 store.register_lints(&DefaultHashTypes::get_lints());
455 store.register_early_pass(|| box DefaultHashTypes::new());
456 store.register_lints(&LintPassImpl::get_lints());
457 store.register_early_pass(|| box LintPassImpl);
458 store.register_lints(&TyTyKind::get_lints());
459 store.register_late_pass(|| box TyTyKind);
460 store.register_group(
465 LintId::of(DEFAULT_HASH_TYPES),
466 LintId::of(USAGE_OF_TY_TYKIND),
467 LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
468 LintId::of(TY_PASS_BY_REFERENCE),
469 LintId::of(USAGE_OF_QUALIFIED_TY),