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)]
18 #![recursion_limit = "256"]
23 extern crate rustc_session;
28 mod nonstandard_style;
29 mod redundant_semicolon;
34 use rustc::hir::def_id::DefId;
36 use rustc::lint::builtin::{
37 BARE_TRAIT_OBJECTS, ELIDED_LIFETIMES_IN_PATHS, EXPLICIT_OUTLIVES_REQUIREMENTS,
38 INTRA_DOC_LINK_RESOLUTION_FAILURE, MISSING_DOC_CODE_EXAMPLES, PRIVATE_DOC_TESTS,
40 use rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintPass};
41 use rustc::ty::query::Providers;
42 use rustc::ty::TyCtxt;
49 use array_into_iter::ArrayIntoIter;
51 use non_ascii_idents::*;
52 use nonstandard_style::*;
53 use redundant_semicolon::*;
54 use rustc::lint::internal::*;
58 /// Useful for other parts of the compiler.
59 pub use builtin::SoftLints;
61 pub fn provide(providers: &mut Providers<'_>) {
62 *providers = Providers { lint_mod, ..*providers };
65 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
66 lint::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
69 macro_rules! pre_expansion_lint_passes {
70 ($macro:path, $args:tt) => {
71 $macro!($args, [KeywordIdents: KeywordIdents, UnusedDocComment: UnusedDocComment,]);
75 macro_rules! early_lint_passes {
76 ($macro:path, $args:tt) => {
80 UnusedParens: UnusedParens,
81 UnusedImportBraces: UnusedImportBraces,
82 UnsafeCode: UnsafeCode,
83 AnonymousParameters: AnonymousParameters,
84 EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
85 NonCamelCaseTypes: NonCamelCaseTypes,
86 DeprecatedAttr: DeprecatedAttr::new(),
88 NonAsciiIdents: NonAsciiIdents,
89 IncompleteFeatures: IncompleteFeatures,
90 RedundantSemicolon: RedundantSemicolon,
96 macro_rules! declare_combined_early_pass {
97 ([$name:ident], $passes:tt) => (
98 early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
102 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
103 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
105 macro_rules! late_lint_passes {
106 ($macro:path, $args:tt) => {
110 // FIXME: Look into regression when this is used as a module lint
111 // May Depend on constants elsewhere
112 UnusedBrokenConst: UnusedBrokenConst,
113 // Uses attr::is_used which is untracked, can't be an incremental module pass.
114 UnusedAttributes: UnusedAttributes::new(),
115 // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
116 UnstableFeatures: UnstableFeatures,
117 // Tracks state across modules
118 UnnameableTestItems: UnnameableTestItems::new(),
119 // Tracks attributes of parents
120 MissingDoc: MissingDoc::new(),
121 // Depends on access levels
122 // FIXME: Turn the computation of types which implement Debug into a query
123 // and change this to a module lint pass
124 MissingDebugImplementations: MissingDebugImplementations::default(),
125 ArrayIntoIter: ArrayIntoIter,
131 macro_rules! late_lint_mod_passes {
132 ($macro:path, $args:tt) => {
136 HardwiredLints: HardwiredLints,
137 ImproperCTypes: ImproperCTypes,
138 VariantSizeDifferences: VariantSizeDifferences,
139 BoxPointers: BoxPointers,
140 PathStatements: PathStatements,
141 // Depends on referenced function signatures in expressions
142 UnusedResults: UnusedResults,
143 NonUpperCaseGlobals: NonUpperCaseGlobals,
144 NonShorthandFieldPatterns: NonShorthandFieldPatterns,
145 UnusedAllocation: UnusedAllocation,
146 // Depends on types used in type definitions
147 MissingCopyImplementations: MissingCopyImplementations,
148 // Depends on referenced function signatures in expressions
149 MutableTransmutes: MutableTransmutes,
150 TypeAliasBounds: TypeAliasBounds,
151 TrivialConstraints: TrivialConstraints,
152 TypeLimits: TypeLimits::new(),
153 NonSnakeCase: NonSnakeCase,
154 InvalidNoMangleItems: InvalidNoMangleItems,
155 // Depends on access levels
156 UnreachablePub: UnreachablePub,
157 ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
158 InvalidValue: InvalidValue,
164 macro_rules! declare_combined_late_pass {
165 ([$v:vis $name:ident], $passes:tt) => (
166 late_lint_methods!(declare_combined_late_lint_pass, [$v $name, $passes], ['tcx]);
170 // FIXME: Make a separate lint type which do not require typeck tables
171 late_lint_passes!(declare_combined_late_pass, [pub BuiltinCombinedLateLintPass]);
173 late_lint_mod_passes!(declare_combined_late_pass, [BuiltinCombinedModuleLateLintPass]);
175 pub fn new_lint_store(no_interleave_lints: bool, internal_lints: bool) -> lint::LintStore {
176 let mut lint_store = lint::LintStore::new();
178 register_builtins(&mut lint_store, no_interleave_lints);
180 register_internals(&mut lint_store);
186 /// Tell the `LintStore` about all the built-in lints (the ones
187 /// defined in this crate and the ones defined in
188 /// `rustc::lint::builtin`).
189 fn register_builtins(store: &mut lint::LintStore, no_interleave_lints: bool) {
190 macro_rules! add_lint_group {
191 ($name:expr, $($lint:ident),*) => (
192 store.register_group(false, $name, None, vec![$(LintId::of($lint)),*]);
196 macro_rules! register_pass {
197 ($method:ident, $ty:ident, $constructor:expr) => {
198 store.register_lints(&$ty::get_lints());
199 store.$method(|| box $constructor);
203 macro_rules! register_passes {
204 ($method:ident, [$($passes:ident: $constructor:expr,)*]) => (
206 register_pass!($method, $passes, $constructor);
211 if no_interleave_lints {
212 pre_expansion_lint_passes!(register_passes, register_pre_expansion_pass);
213 early_lint_passes!(register_passes, register_early_pass);
214 late_lint_passes!(register_passes, register_late_pass);
215 late_lint_mod_passes!(register_passes, register_late_mod_pass);
217 store.register_lints(&BuiltinCombinedPreExpansionLintPass::get_lints());
218 store.register_lints(&BuiltinCombinedEarlyLintPass::get_lints());
219 store.register_lints(&BuiltinCombinedModuleLateLintPass::get_lints());
220 store.register_lints(&BuiltinCombinedLateLintPass::get_lints());
225 NON_CAMEL_CASE_TYPES,
227 NON_UPPER_CASE_GLOBALS
238 UNREACHABLE_PATTERNS,
239 OVERLAPPING_PATTERNS,
247 UNUSED_EXTERN_CRATES,
256 UNUSED_EXTERN_CRATES,
257 ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
258 ELIDED_LIFETIMES_IN_PATHS,
259 EXPLICIT_OUTLIVES_REQUIREMENTS // FIXME(#52665, #47816) not always applicable and not all
260 // macros are ready for this yet.
263 // FIXME macro crates are not up for this yet, too much
264 // breakage is seen if we try to encourage this lint.
265 // MACRO_USE_EXTERN_CRATE
270 INTRA_DOC_LINK_RESOLUTION_FAILURE,
271 MISSING_DOC_CODE_EXAMPLES,
275 // Register renamed and removed lints.
276 store.register_renamed("single_use_lifetime", "single_use_lifetimes");
277 store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
278 store.register_renamed("bare_trait_object", "bare_trait_objects");
279 store.register_renamed("unstable_name_collision", "unstable_name_collisions");
280 store.register_renamed("unused_doc_comment", "unused_doc_comments");
281 store.register_renamed("async_idents", "keyword_idents");
282 store.register_removed("unknown_features", "replaced by an error");
283 store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
284 store.register_removed("negate_unsigned", "cast a signed value instead");
285 store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
286 // Register lint group aliases.
287 store.register_group_alias("nonstandard_style", "bad_style");
288 // This was renamed to `raw_pointer_derive`, which was then removed,
289 // so it is also considered removed.
290 store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
291 store.register_removed("drop_with_repr_extern", "drop flags have been removed");
292 store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
293 store.register_removed("deprecated_attr", "use `deprecated` instead");
294 store.register_removed(
295 "transmute_from_fn_item_types",
296 "always cast functions before transmuting them",
298 store.register_removed(
299 "hr_lifetime_in_assoc_type",
300 "converted into hard error, see https://github.com/rust-lang/rust/issues/33685",
302 store.register_removed(
303 "inaccessible_extern_crate",
304 "converted into hard error, see https://github.com/rust-lang/rust/issues/36886",
306 store.register_removed(
307 "super_or_self_in_global_path",
308 "converted into hard error, see https://github.com/rust-lang/rust/issues/36888",
310 store.register_removed(
311 "overlapping_inherent_impls",
312 "converted into hard error, see https://github.com/rust-lang/rust/issues/36889",
314 store.register_removed(
315 "illegal_floating_point_constant_pattern",
316 "converted into hard error, see https://github.com/rust-lang/rust/issues/36890",
318 store.register_removed(
319 "illegal_struct_or_enum_constant_pattern",
320 "converted into hard error, see https://github.com/rust-lang/rust/issues/36891",
322 store.register_removed(
323 "lifetime_underscore",
324 "converted into hard error, see https://github.com/rust-lang/rust/issues/36892",
326 store.register_removed(
327 "extra_requirement_in_impl",
328 "converted into hard error, see https://github.com/rust-lang/rust/issues/37166",
330 store.register_removed(
332 "converted into hard error, see https://github.com/rust-lang/rust/issues/38260",
334 store.register_removed(
336 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950",
338 store.register_removed(
339 "resolve_trait_on_defaulted_unit",
340 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950",
342 store.register_removed(
343 "private_no_mangle_fns",
344 "no longer a warning, `#[no_mangle]` functions always exported",
346 store.register_removed(
347 "private_no_mangle_statics",
348 "no longer a warning, `#[no_mangle]` statics always exported",
350 store.register_removed("bad_repr", "replaced with a generic attribute input check");
351 store.register_removed(
352 "duplicate_matcher_binding_name",
353 "converted into hard error, see https://github.com/rust-lang/rust/issues/57742",
355 store.register_removed(
356 "incoherent_fundamental_impls",
357 "converted into hard error, see https://github.com/rust-lang/rust/issues/46205",
359 store.register_removed(
360 "legacy_constructor_visibility",
361 "converted into hard error, see https://github.com/rust-lang/rust/issues/39207",
363 store.register_removed(
364 "legacy_directory_ownership",
365 "converted into hard error, see https://github.com/rust-lang/rust/issues/37872",
367 store.register_removed(
368 "safe_extern_statics",
369 "converted into hard error, see https://github.com/rust-lang/rust/issues/36247",
371 store.register_removed(
372 "parenthesized_params_in_types_and_modules",
373 "converted into hard error, see https://github.com/rust-lang/rust/issues/42238",
375 store.register_removed(
376 "duplicate_macro_exports",
377 "converted into hard error, see https://github.com/rust-lang/rust/issues/35896",
379 store.register_removed(
381 "converted into hard error, see https://github.com/rust-lang/rust/issues/59014",
383 store.register_removed("plugin_as_library", "plugins have been deprecated and retired");
386 fn register_internals(store: &mut lint::LintStore) {
387 store.register_lints(&DefaultHashTypes::get_lints());
388 store.register_early_pass(|| box DefaultHashTypes::new());
389 store.register_lints(&LintPassImpl::get_lints());
390 store.register_early_pass(|| box LintPassImpl);
391 store.register_lints(&TyTyKind::get_lints());
392 store.register_late_pass(|| box TyTyKind);
393 store.register_group(
398 LintId::of(DEFAULT_HASH_TYPES),
399 LintId::of(USAGE_OF_TY_TYKIND),
400 LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
401 LintId::of(TY_PASS_BY_REFERENCE),
402 LintId::of(USAGE_OF_QUALIFIED_TY),