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/")]
13 #![cfg_attr(test, feature(test))]
14 #![feature(bool_to_option)]
15 #![feature(box_patterns)]
16 #![feature(box_syntax)]
17 #![feature(crate_visibility_modifier)]
19 #![recursion_limit = "256"]
24 extern crate rustc_session;
33 mod nonstandard_style;
34 mod redundant_semicolon;
39 use rustc::lint::builtin::{
40 BARE_TRAIT_OBJECTS, ELIDED_LIFETIMES_IN_PATHS, EXPLICIT_OUTLIVES_REQUIREMENTS,
41 INTRA_DOC_LINK_RESOLUTION_FAILURE, MISSING_DOC_CODE_EXAMPLES, PRIVATE_DOC_TESTS,
43 use rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
44 use rustc::ty::query::Providers;
45 use rustc::ty::TyCtxt;
47 use rustc_hir::def_id::DefId;
48 use rustc_session::lint::{LintArray, LintPass};
55 use array_into_iter::ArrayIntoIter;
58 use non_ascii_idents::*;
59 use nonstandard_style::*;
60 use redundant_semicolon::*;
64 /// Useful for other parts of the compiler.
65 pub use builtin::SoftLints;
66 pub use early::check_ast_crate;
67 pub use late::check_crate;
69 pub fn provide(providers: &mut Providers<'_>) {
70 levels::provide(providers);
71 *providers = Providers { lint_mod, ..*providers };
74 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
75 late::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
78 macro_rules! pre_expansion_lint_passes {
79 ($macro:path, $args:tt) => {
80 $macro!($args, [KeywordIdents: KeywordIdents, UnusedDocComment: UnusedDocComment,]);
84 macro_rules! early_lint_passes {
85 ($macro:path, $args:tt) => {
89 UnusedParens: UnusedParens,
90 UnusedImportBraces: UnusedImportBraces,
91 UnsafeCode: UnsafeCode,
92 AnonymousParameters: AnonymousParameters,
93 EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
94 NonCamelCaseTypes: NonCamelCaseTypes,
95 DeprecatedAttr: DeprecatedAttr::new(),
97 NonAsciiIdents: NonAsciiIdents,
98 IncompleteFeatures: IncompleteFeatures,
99 RedundantSemicolon: RedundantSemicolon,
105 macro_rules! declare_combined_early_pass {
106 ([$name:ident], $passes:tt) => (
107 early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
111 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
112 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
114 macro_rules! late_lint_passes {
115 ($macro:path, $args:tt) => {
119 // FIXME: Look into regression when this is used as a module lint
120 // May Depend on constants elsewhere
121 UnusedBrokenConst: UnusedBrokenConst,
122 // Uses attr::is_used which is untracked, can't be an incremental module pass.
123 UnusedAttributes: UnusedAttributes::new(),
124 // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
125 UnstableFeatures: UnstableFeatures,
126 // Tracks state across modules
127 UnnameableTestItems: UnnameableTestItems::new(),
128 // Tracks attributes of parents
129 MissingDoc: MissingDoc::new(),
130 // Depends on access levels
131 // FIXME: Turn the computation of types which implement Debug into a query
132 // and change this to a module lint pass
133 MissingDebugImplementations: MissingDebugImplementations::default(),
134 ArrayIntoIter: ArrayIntoIter,
140 macro_rules! late_lint_mod_passes {
141 ($macro:path, $args:tt) => {
145 HardwiredLints: HardwiredLints,
146 ImproperCTypes: ImproperCTypes,
147 VariantSizeDifferences: VariantSizeDifferences,
148 BoxPointers: BoxPointers,
149 PathStatements: PathStatements,
150 // Depends on referenced function signatures in expressions
151 UnusedResults: UnusedResults,
152 NonUpperCaseGlobals: NonUpperCaseGlobals,
153 NonShorthandFieldPatterns: NonShorthandFieldPatterns,
154 UnusedAllocation: UnusedAllocation,
155 // Depends on types used in type definitions
156 MissingCopyImplementations: MissingCopyImplementations,
157 // Depends on referenced function signatures in expressions
158 MutableTransmutes: MutableTransmutes,
159 TypeAliasBounds: TypeAliasBounds,
160 TrivialConstraints: TrivialConstraints,
161 TypeLimits: TypeLimits::new(),
162 NonSnakeCase: NonSnakeCase,
163 InvalidNoMangleItems: InvalidNoMangleItems,
164 // Depends on access levels
165 UnreachablePub: UnreachablePub,
166 ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
167 InvalidValue: InvalidValue,
173 macro_rules! declare_combined_late_pass {
174 ([$v:vis $name:ident], $passes:tt) => (
175 late_lint_methods!(declare_combined_late_lint_pass, [$v $name, $passes], ['tcx]);
179 // FIXME: Make a separate lint type which do not require typeck tables
180 late_lint_passes!(declare_combined_late_pass, [pub BuiltinCombinedLateLintPass]);
182 late_lint_mod_passes!(declare_combined_late_pass, [BuiltinCombinedModuleLateLintPass]);
184 pub fn new_lint_store(no_interleave_lints: bool, internal_lints: bool) -> lint::LintStore {
185 let mut lint_store = lint::LintStore::new();
187 register_builtins(&mut lint_store, no_interleave_lints);
189 register_internals(&mut lint_store);
195 /// Tell the `LintStore` about all the built-in lints (the ones
196 /// defined in this crate and the ones defined in
197 /// `rustc::lint::builtin`).
198 fn register_builtins(store: &mut lint::LintStore, no_interleave_lints: bool) {
199 macro_rules! add_lint_group {
200 ($name:expr, $($lint:ident),*) => (
201 store.register_group(false, $name, None, vec![$(LintId::of($lint)),*]);
205 macro_rules! register_pass {
206 ($method:ident, $ty:ident, $constructor:expr) => {
207 store.register_lints(&$ty::get_lints());
208 store.$method(|| box $constructor);
212 macro_rules! register_passes {
213 ($method:ident, [$($passes:ident: $constructor:expr,)*]) => (
215 register_pass!($method, $passes, $constructor);
220 if no_interleave_lints {
221 pre_expansion_lint_passes!(register_passes, register_pre_expansion_pass);
222 early_lint_passes!(register_passes, register_early_pass);
223 late_lint_passes!(register_passes, register_late_pass);
224 late_lint_mod_passes!(register_passes, register_late_mod_pass);
226 store.register_lints(&BuiltinCombinedPreExpansionLintPass::get_lints());
227 store.register_lints(&BuiltinCombinedEarlyLintPass::get_lints());
228 store.register_lints(&BuiltinCombinedModuleLateLintPass::get_lints());
229 store.register_lints(&BuiltinCombinedLateLintPass::get_lints());
234 NON_CAMEL_CASE_TYPES,
236 NON_UPPER_CASE_GLOBALS
247 UNREACHABLE_PATTERNS,
248 OVERLAPPING_PATTERNS,
256 UNUSED_EXTERN_CRATES,
265 UNUSED_EXTERN_CRATES,
266 ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
267 ELIDED_LIFETIMES_IN_PATHS,
268 EXPLICIT_OUTLIVES_REQUIREMENTS // FIXME(#52665, #47816) not always applicable and not all
269 // macros are ready for this yet.
272 // FIXME macro crates are not up for this yet, too much
273 // breakage is seen if we try to encourage this lint.
274 // MACRO_USE_EXTERN_CRATE
279 INTRA_DOC_LINK_RESOLUTION_FAILURE,
280 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(
304 "transmute_from_fn_item_types",
305 "always cast functions before transmuting them",
307 store.register_removed(
308 "hr_lifetime_in_assoc_type",
309 "converted into hard error, see https://github.com/rust-lang/rust/issues/33685",
311 store.register_removed(
312 "inaccessible_extern_crate",
313 "converted into hard error, see https://github.com/rust-lang/rust/issues/36886",
315 store.register_removed(
316 "super_or_self_in_global_path",
317 "converted into hard error, see https://github.com/rust-lang/rust/issues/36888",
319 store.register_removed(
320 "overlapping_inherent_impls",
321 "converted into hard error, see https://github.com/rust-lang/rust/issues/36889",
323 store.register_removed(
324 "illegal_floating_point_constant_pattern",
325 "converted into hard error, see https://github.com/rust-lang/rust/issues/36890",
327 store.register_removed(
328 "illegal_struct_or_enum_constant_pattern",
329 "converted into hard error, see https://github.com/rust-lang/rust/issues/36891",
331 store.register_removed(
332 "lifetime_underscore",
333 "converted into hard error, see https://github.com/rust-lang/rust/issues/36892",
335 store.register_removed(
336 "extra_requirement_in_impl",
337 "converted into hard error, see https://github.com/rust-lang/rust/issues/37166",
339 store.register_removed(
341 "converted into hard error, see https://github.com/rust-lang/rust/issues/38260",
343 store.register_removed(
345 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950",
347 store.register_removed(
348 "resolve_trait_on_defaulted_unit",
349 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950",
351 store.register_removed(
352 "private_no_mangle_fns",
353 "no longer a warning, `#[no_mangle]` functions always exported",
355 store.register_removed(
356 "private_no_mangle_statics",
357 "no longer a warning, `#[no_mangle]` statics always exported",
359 store.register_removed("bad_repr", "replaced with a generic attribute input check");
360 store.register_removed(
361 "duplicate_matcher_binding_name",
362 "converted into hard error, see https://github.com/rust-lang/rust/issues/57742",
364 store.register_removed(
365 "incoherent_fundamental_impls",
366 "converted into hard error, see https://github.com/rust-lang/rust/issues/46205",
368 store.register_removed(
369 "legacy_constructor_visibility",
370 "converted into hard error, see https://github.com/rust-lang/rust/issues/39207",
372 store.register_removed(
373 "legacy_directory_ownership",
374 "converted into hard error, see https://github.com/rust-lang/rust/issues/37872",
376 store.register_removed(
377 "safe_extern_statics",
378 "converted into hard error, see https://github.com/rust-lang/rust/issues/36247",
380 store.register_removed(
381 "parenthesized_params_in_types_and_modules",
382 "converted into hard error, see https://github.com/rust-lang/rust/issues/42238",
384 store.register_removed(
385 "duplicate_macro_exports",
386 "converted into hard error, see https://github.com/rust-lang/rust/issues/35896",
388 store.register_removed(
390 "converted into hard error, see https://github.com/rust-lang/rust/issues/59014",
392 store.register_removed("plugin_as_library", "plugins have been deprecated and retired");
395 fn register_internals(store: &mut lint::LintStore) {
396 store.register_lints(&DefaultHashTypes::get_lints());
397 store.register_early_pass(|| box DefaultHashTypes::new());
398 store.register_lints(&LintPassImpl::get_lints());
399 store.register_early_pass(|| box LintPassImpl);
400 store.register_lints(&TyTyKind::get_lints());
401 store.register_late_pass(|| box TyTyKind);
402 store.register_group(
407 LintId::of(DEFAULT_HASH_TYPES),
408 LintId::of(USAGE_OF_TY_TYKIND),
409 LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
410 LintId::of(TY_PASS_BY_REFERENCE),
411 LintId::of(USAGE_OF_QUALIFIED_TY),