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_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)]
18 #![feature(rustc_diagnostic_macros)]
20 #![recursion_limit="256"]
26 mod nonstandard_style;
33 use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
34 use rustc::lint::builtin::{
36 ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
37 ELIDED_LIFETIMES_IN_PATHS,
38 EXPLICIT_OUTLIVES_REQUIREMENTS,
39 INTRA_DOC_LINK_RESOLUTION_FAILURE,
40 MISSING_DOC_CODE_EXAMPLES,
42 parser::ILL_FORMED_ATTRIBUTE_INPUT,
46 use rustc::hir::def_id::DefId;
47 use rustc::ty::query::Providers;
48 use rustc::ty::TyCtxt;
51 use syntax::edition::Edition;
56 use lint::FutureIncompatibleInfo;
58 use nonstandard_style::*;
62 use non_ascii_idents::*;
63 use rustc::lint::internal::*;
65 /// Useful for other parts of the compiler.
66 pub use builtin::SoftLints;
68 pub fn provide(providers: &mut Providers<'_>) {
69 *providers = Providers {
75 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
76 lint::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
79 macro_rules! pre_expansion_lint_passes {
80 ($macro:path, $args:tt) => (
82 KeywordIdents: KeywordIdents,
83 UnusedDocComment: UnusedDocComment,
88 macro_rules! early_lint_passes {
89 ($macro:path, $args:tt) => (
91 UnusedParens: UnusedParens,
92 UnusedImportBraces: UnusedImportBraces,
93 UnsafeCode: UnsafeCode,
94 AnonymousParameters: AnonymousParameters,
95 EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
96 NonCamelCaseTypes: NonCamelCaseTypes,
97 DeprecatedAttr: DeprecatedAttr::new(),
99 NonAsciiIdents: NonAsciiIdents,
104 macro_rules! declare_combined_early_pass {
105 ([$name:ident], $passes:tt) => (
106 early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
110 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
111 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
113 macro_rules! late_lint_passes {
114 ($macro:path, $args:tt) => (
116 // FIXME: Look into regression when this is used as a module lint
117 // May Depend on constants elsewhere
118 UnusedBrokenConst: UnusedBrokenConst,
120 // Uses attr::is_used which is untracked, can't be an incremental module pass.
121 UnusedAttributes: UnusedAttributes::new(),
123 // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
124 UnstableFeatures: UnstableFeatures,
126 // Tracks state across modules
127 UnnameableTestItems: UnnameableTestItems::new(),
129 // Tracks attributes of parents
130 MissingDoc: MissingDoc::new(),
132 // Depends on access levels
133 // FIXME: Turn the computation of types which implement Debug into a query
134 // and change this to a module lint pass
135 MissingDebugImplementations: MissingDebugImplementations::default(),
140 macro_rules! late_lint_mod_passes {
141 ($macro:path, $args:tt) => (
143 HardwiredLints: HardwiredLints,
144 ImproperCTypes: ImproperCTypes,
145 VariantSizeDifferences: VariantSizeDifferences,
146 BoxPointers: BoxPointers,
147 PathStatements: PathStatements,
149 // Depends on referenced function signatures in expressions
150 UnusedResults: UnusedResults,
152 NonUpperCaseGlobals: NonUpperCaseGlobals,
153 NonShorthandFieldPatterns: NonShorthandFieldPatterns,
154 UnusedAllocation: UnusedAllocation,
156 // Depends on types used in type definitions
157 MissingCopyImplementations: MissingCopyImplementations,
159 PluginAsLibrary: PluginAsLibrary,
161 // Depends on referenced function signatures in expressions
162 MutableTransmutes: MutableTransmutes,
164 // Depends on types of fields, checks if they implement Drop
165 UnionsWithDropFields: UnionsWithDropFields,
167 TypeAliasBounds: TypeAliasBounds,
169 TrivialConstraints: TrivialConstraints,
170 TypeLimits: TypeLimits::new(),
172 NonSnakeCase: NonSnakeCase,
173 InvalidNoMangleItems: InvalidNoMangleItems,
175 // Depends on access levels
176 UnreachablePub: UnreachablePub,
178 ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
183 macro_rules! declare_combined_late_pass {
184 ([$v:vis $name:ident], $passes:tt) => (
185 late_lint_methods!(declare_combined_late_lint_pass, [$v $name, $passes], ['tcx]);
189 // FIXME: Make a separate lint type which do not require typeck tables
190 late_lint_passes!(declare_combined_late_pass, [pub BuiltinCombinedLateLintPass]);
192 late_lint_mod_passes!(declare_combined_late_pass, [BuiltinCombinedModuleLateLintPass]);
194 /// Tell the `LintStore` about all the built-in lints (the ones
195 /// defined in this crate and the ones defined in
196 /// `rustc::lint::builtin`).
197 pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
198 macro_rules! add_lint_group {
199 ($sess:ident, $name:expr, $($lint:ident),*) => (
200 store.register_group($sess, false, $name, None, vec![$(LintId::of($lint)),*]);
204 macro_rules! register_pass {
205 ($method:ident, $constructor:expr, [$($args:expr),*]) => (
206 store.$method(sess, false, false, $($args,)* box $constructor);
210 macro_rules! register_passes {
211 ([$method:ident, $args:tt], [$($passes:ident: $constructor:expr,)*]) => (
213 register_pass!($method, $constructor, $args);
218 if sess.map(|sess| sess.opts.debugging_opts.no_interleave_lints).unwrap_or(false) {
219 pre_expansion_lint_passes!(register_passes, [register_pre_expansion_pass, []]);
220 early_lint_passes!(register_passes, [register_early_pass, []]);
221 late_lint_passes!(register_passes, [register_late_pass, [false]]);
222 late_lint_mod_passes!(register_passes, [register_late_pass, [true]]);
224 store.register_pre_expansion_pass(
228 box BuiltinCombinedPreExpansionLintPass::new()
230 store.register_early_pass(sess, false, true, box BuiltinCombinedEarlyLintPass::new());
231 store.register_late_pass(
232 sess, false, true, true, box BuiltinCombinedModuleLateLintPass::new()
234 store.register_late_pass(
235 sess, false, true, false, box BuiltinCombinedLateLintPass::new()
239 add_lint_group!(sess,
241 NON_CAMEL_CASE_TYPES,
243 NON_UPPER_CASE_GLOBALS);
245 add_lint_group!(sess,
253 UNREACHABLE_PATTERNS,
261 UNUSED_EXTERN_CRATES,
266 add_lint_group!(sess,
269 UNUSED_EXTERN_CRATES,
270 ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
271 ELIDED_LIFETIMES_IN_PATHS,
272 EXPLICIT_OUTLIVES_REQUIREMENTS
274 // FIXME(#52665, #47816) not always applicable and not all
275 // macros are ready for this yet.
278 // FIXME macro crates are not up for this yet, too much
279 // breakage is seen if we try to encourage this lint.
280 // MACRO_USE_EXTERN_CRATE,
283 add_lint_group!(sess,
285 INTRA_DOC_LINK_RESOLUTION_FAILURE,
286 MISSING_DOC_CODE_EXAMPLES,
289 // Guidelines for creating a future incompatibility lint:
291 // - Create a lint defaulting to warn as normal, with ideally the same error
292 // message you would normally give
293 // - Add a suitable reference, typically an RFC or tracking issue. Go ahead
294 // and include the full URL, sort items in ascending order of issue numbers.
295 // - Later, change lint to error
296 // - Eventually, remove lint
297 store.register_future_incompatible(sess, vec![
298 FutureIncompatibleInfo {
299 id: LintId::of(PRIVATE_IN_PUBLIC),
300 reference: "issue #34537 <https://github.com/rust-lang/rust/issues/34537>",
303 FutureIncompatibleInfo {
304 id: LintId::of(PUB_USE_OF_PRIVATE_EXTERN_CRATE),
305 reference: "issue #34537 <https://github.com/rust-lang/rust/issues/34537>",
308 FutureIncompatibleInfo {
309 id: LintId::of(PATTERNS_IN_FNS_WITHOUT_BODY),
310 reference: "issue #35203 <https://github.com/rust-lang/rust/issues/35203>",
313 FutureIncompatibleInfo {
314 id: LintId::of(DUPLICATE_MACRO_EXPORTS),
315 reference: "issue #35896 <https://github.com/rust-lang/rust/issues/35896>",
316 edition: Some(Edition::Edition2018),
318 FutureIncompatibleInfo {
319 id: LintId::of(KEYWORD_IDENTS),
320 reference: "issue #49716 <https://github.com/rust-lang/rust/issues/49716>",
321 edition: Some(Edition::Edition2018),
323 FutureIncompatibleInfo {
324 id: LintId::of(SAFE_EXTERN_STATICS),
325 reference: "issue #36247 <https://github.com/rust-lang/rust/issues/36247>",
328 FutureIncompatibleInfo {
329 id: LintId::of(INVALID_TYPE_PARAM_DEFAULT),
330 reference: "issue #36887 <https://github.com/rust-lang/rust/issues/36887>",
333 FutureIncompatibleInfo {
334 id: LintId::of(LEGACY_DIRECTORY_OWNERSHIP),
335 reference: "issue #37872 <https://github.com/rust-lang/rust/issues/37872>",
338 FutureIncompatibleInfo {
339 id: LintId::of(LEGACY_CONSTRUCTOR_VISIBILITY),
340 reference: "issue #39207 <https://github.com/rust-lang/rust/issues/39207>",
343 FutureIncompatibleInfo {
344 id: LintId::of(MISSING_FRAGMENT_SPECIFIER),
345 reference: "issue #40107 <https://github.com/rust-lang/rust/issues/40107>",
348 FutureIncompatibleInfo {
349 id: LintId::of(ILLEGAL_FLOATING_POINT_LITERAL_PATTERN),
350 reference: "issue #41620 <https://github.com/rust-lang/rust/issues/41620>",
353 FutureIncompatibleInfo {
354 id: LintId::of(ANONYMOUS_PARAMETERS),
355 reference: "issue #41686 <https://github.com/rust-lang/rust/issues/41686>",
356 edition: Some(Edition::Edition2018),
358 FutureIncompatibleInfo {
359 id: LintId::of(PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES),
360 reference: "issue #42238 <https://github.com/rust-lang/rust/issues/42238>",
363 FutureIncompatibleInfo {
364 id: LintId::of(LATE_BOUND_LIFETIME_ARGUMENTS),
365 reference: "issue #42868 <https://github.com/rust-lang/rust/issues/42868>",
368 FutureIncompatibleInfo {
369 id: LintId::of(SAFE_PACKED_BORROWS),
370 reference: "issue #46043 <https://github.com/rust-lang/rust/issues/46043>",
373 FutureIncompatibleInfo {
374 id: LintId::of(ORDER_DEPENDENT_TRAIT_OBJECTS),
375 reference: "issue #56484 <https://github.com/rust-lang/rust/issues/56484>",
378 FutureIncompatibleInfo {
379 id: LintId::of(TYVAR_BEHIND_RAW_POINTER),
380 reference: "issue #46906 <https://github.com/rust-lang/rust/issues/46906>",
381 edition: Some(Edition::Edition2018),
383 FutureIncompatibleInfo {
384 id: LintId::of(UNSTABLE_NAME_COLLISIONS),
385 reference: "issue #48919 <https://github.com/rust-lang/rust/issues/48919>",
387 // Note: this item represents future incompatibility of all unstable functions in the
388 // standard library, and thus should never be removed or changed to an error.
390 FutureIncompatibleInfo {
391 id: LintId::of(ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE),
392 reference: "issue #53130 <https://github.com/rust-lang/rust/issues/53130>",
393 edition: Some(Edition::Edition2018),
395 FutureIncompatibleInfo {
396 id: LintId::of(WHERE_CLAUSES_OBJECT_SAFETY),
397 reference: "issue #51443 <https://github.com/rust-lang/rust/issues/51443>",
400 FutureIncompatibleInfo {
401 id: LintId::of(PROC_MACRO_DERIVE_RESOLUTION_FALLBACK),
402 reference: "issue #50504 <https://github.com/rust-lang/rust/issues/50504>",
405 FutureIncompatibleInfo {
406 id: LintId::of(MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS),
407 reference: "issue #52234 <https://github.com/rust-lang/rust/issues/52234>",
410 FutureIncompatibleInfo {
411 id: LintId::of(ILL_FORMED_ATTRIBUTE_INPUT),
412 reference: "issue #57571 <https://github.com/rust-lang/rust/issues/57571>",
415 FutureIncompatibleInfo {
416 id: LintId::of(AMBIGUOUS_ASSOCIATED_ITEMS),
417 reference: "issue #57644 <https://github.com/rust-lang/rust/issues/57644>",
420 FutureIncompatibleInfo {
421 id: LintId::of(NESTED_IMPL_TRAIT),
422 reference: "issue #59014 <https://github.com/rust-lang/rust/issues/59014>",
425 FutureIncompatibleInfo {
426 id: LintId::of(MUTABLE_BORROW_RESERVATION_CONFLICT),
427 reference: "issue #59159 <https://github.com/rust-lang/rust/issues/59159>",
430 FutureIncompatibleInfo {
431 id: LintId::of(INDIRECT_STRUCTURAL_MATCH),
432 reference: "issue #62411 <https://github.com/rust-lang/rust/issues/62411>",
437 // Register renamed and removed lints.
438 store.register_renamed("single_use_lifetime", "single_use_lifetimes");
439 store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
440 store.register_renamed("bare_trait_object", "bare_trait_objects");
441 store.register_renamed("unstable_name_collision", "unstable_name_collisions");
442 store.register_renamed("unused_doc_comment", "unused_doc_comments");
443 store.register_renamed("async_idents", "keyword_idents");
444 store.register_removed("unknown_features", "replaced by an error");
445 store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
446 store.register_removed("negate_unsigned", "cast a signed value instead");
447 store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
448 // Register lint group aliases.
449 store.register_group_alias("nonstandard_style", "bad_style");
450 // This was renamed to `raw_pointer_derive`, which was then removed,
451 // so it is also considered removed.
452 store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
453 store.register_removed("drop_with_repr_extern", "drop flags have been removed");
454 store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
455 store.register_removed("deprecated_attr", "use `deprecated` instead");
456 store.register_removed("transmute_from_fn_item_types",
457 "always cast functions before transmuting them");
458 store.register_removed("hr_lifetime_in_assoc_type",
459 "converted into hard error, see https://github.com/rust-lang/rust/issues/33685");
460 store.register_removed("inaccessible_extern_crate",
461 "converted into hard error, see https://github.com/rust-lang/rust/issues/36886");
462 store.register_removed("super_or_self_in_global_path",
463 "converted into hard error, see https://github.com/rust-lang/rust/issues/36888");
464 store.register_removed("overlapping_inherent_impls",
465 "converted into hard error, see https://github.com/rust-lang/rust/issues/36889");
466 store.register_removed("illegal_floating_point_constant_pattern",
467 "converted into hard error, see https://github.com/rust-lang/rust/issues/36890");
468 store.register_removed("illegal_struct_or_enum_constant_pattern",
469 "converted into hard error, see https://github.com/rust-lang/rust/issues/36891");
470 store.register_removed("lifetime_underscore",
471 "converted into hard error, see https://github.com/rust-lang/rust/issues/36892");
472 store.register_removed("extra_requirement_in_impl",
473 "converted into hard error, see https://github.com/rust-lang/rust/issues/37166");
474 store.register_removed("legacy_imports",
475 "converted into hard error, see https://github.com/rust-lang/rust/issues/38260");
476 store.register_removed("coerce_never",
477 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
478 store.register_removed("resolve_trait_on_defaulted_unit",
479 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
480 store.register_removed("private_no_mangle_fns",
481 "no longer a warning, `#[no_mangle]` functions always exported");
482 store.register_removed("private_no_mangle_statics",
483 "no longer a warning, `#[no_mangle]` statics always exported");
484 store.register_removed("bad_repr",
485 "replaced with a generic attribute input check");
486 store.register_removed("duplicate_matcher_binding_name",
487 "converted into hard error, see https://github.com/rust-lang/rust/issues/57742");
488 store.register_removed("incoherent_fundamental_impls",
489 "converted into hard error, see https://github.com/rust-lang/rust/issues/46205");
492 pub fn register_internals(store: &mut lint::LintStore, sess: Option<&Session>) {
493 store.register_early_pass(sess, false, false, box DefaultHashTypes::new());
494 store.register_early_pass(sess, false, false, box LintPassImpl);
495 store.register_late_pass(sess, false, false, false, box TyTyKind);
496 store.register_group(
502 LintId::of(DEFAULT_HASH_TYPES),
503 LintId::of(USAGE_OF_TY_TYKIND),
504 LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
505 LintId::of(TY_PASS_BY_REFERENCE),
506 LintId::of(USAGE_OF_QUALIFIED_TY),