]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/lib.rs
Rollup merge of #67102 - Aaron1011:patch-3, r=Mark-Simulacrum
[rust.git] / src / librustc_lint / lib.rs
1 //! # Lints in the Rust compiler
2 //!
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.
7 //!
8 //! ## Note
9 //!
10 //! This API is completely unstable and subject to change.
11
12 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
13
14 #![cfg_attr(test, feature(test))]
15 #![feature(bool_to_option)]
16 #![feature(box_patterns)]
17 #![feature(box_syntax)]
18 #![feature(nll)]
19 #![feature(matches_macro)]
20
21 #![recursion_limit="256"]
22
23 #[macro_use]
24 extern crate rustc;
25 #[macro_use]
26 extern crate rustc_session;
27
28 mod array_into_iter;
29 mod nonstandard_style;
30 mod redundant_semicolon;
31 pub mod builtin;
32 mod types;
33 mod unused;
34 mod non_ascii_idents;
35
36 use rustc::lint;
37 use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
38 use rustc::lint::builtin::{
39     BARE_TRAIT_OBJECTS,
40     ELIDED_LIFETIMES_IN_PATHS,
41     EXPLICIT_OUTLIVES_REQUIREMENTS,
42     INTRA_DOC_LINK_RESOLUTION_FAILURE,
43     MISSING_DOC_CODE_EXAMPLES,
44     PRIVATE_DOC_TESTS,
45 };
46 use rustc::hir;
47 use rustc::hir::def_id::DefId;
48 use rustc::ty::query::Providers;
49 use rustc::ty::TyCtxt;
50
51 use syntax::ast;
52 use syntax_pos::Span;
53
54 use lint::LintId;
55
56 use redundant_semicolon::*;
57 use nonstandard_style::*;
58 use builtin::*;
59 use types::*;
60 use unused::*;
61 use non_ascii_idents::*;
62 use rustc::lint::internal::*;
63 use array_into_iter::ArrayIntoIter;
64
65 /// Useful for other parts of the compiler.
66 pub use builtin::SoftLints;
67
68 pub fn provide(providers: &mut Providers<'_>) {
69     *providers = Providers {
70         lint_mod,
71         ..*providers
72     };
73 }
74
75 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
76     lint::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
77 }
78
79 macro_rules! pre_expansion_lint_passes {
80     ($macro:path, $args:tt) => (
81         $macro!($args, [
82             KeywordIdents: KeywordIdents,
83             UnusedDocComment: UnusedDocComment,
84         ]);
85     )
86 }
87
88 macro_rules! early_lint_passes {
89     ($macro:path, $args:tt) => (
90         $macro!($args, [
91             UnusedParens: UnusedParens,
92             UnusedImportBraces: UnusedImportBraces,
93             UnsafeCode: UnsafeCode,
94             AnonymousParameters: AnonymousParameters,
95             EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
96             NonCamelCaseTypes: NonCamelCaseTypes,
97             DeprecatedAttr: DeprecatedAttr::new(),
98             WhileTrue: WhileTrue,
99             NonAsciiIdents: NonAsciiIdents,
100             IncompleteFeatures: IncompleteFeatures,
101             RedundantSemicolon: RedundantSemicolon,
102         ]);
103     )
104 }
105
106 macro_rules! declare_combined_early_pass {
107     ([$name:ident], $passes:tt) => (
108         early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
109     )
110 }
111
112 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
113 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
114
115 macro_rules! late_lint_passes {
116     ($macro:path, $args:tt) => (
117         $macro!($args, [
118             // FIXME: Look into regression when this is used as a module lint
119             // May Depend on constants elsewhere
120             UnusedBrokenConst: UnusedBrokenConst,
121
122             // Uses attr::is_used which is untracked, can't be an incremental module pass.
123             UnusedAttributes: UnusedAttributes::new(),
124
125             // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
126             UnstableFeatures: UnstableFeatures,
127
128             // Tracks state across modules
129             UnnameableTestItems: UnnameableTestItems::new(),
130
131             // Tracks attributes of parents
132             MissingDoc: MissingDoc::new(),
133
134             // Depends on access levels
135             // FIXME: Turn the computation of types which implement Debug into a query
136             // and change this to a module lint pass
137             MissingDebugImplementations: MissingDebugImplementations::default(),
138
139             ArrayIntoIter: ArrayIntoIter,
140         ]);
141     )
142 }
143
144 macro_rules! late_lint_mod_passes {
145     ($macro:path, $args:tt) => (
146         $macro!($args, [
147             HardwiredLints: HardwiredLints,
148             ImproperCTypes: ImproperCTypes,
149             VariantSizeDifferences: VariantSizeDifferences,
150             BoxPointers: BoxPointers,
151             PathStatements: PathStatements,
152
153             // Depends on referenced function signatures in expressions
154             UnusedResults: UnusedResults,
155
156             NonUpperCaseGlobals: NonUpperCaseGlobals,
157             NonShorthandFieldPatterns: NonShorthandFieldPatterns,
158             UnusedAllocation: UnusedAllocation,
159
160             // Depends on types used in type definitions
161             MissingCopyImplementations: MissingCopyImplementations,
162
163             // Depends on referenced function signatures in expressions
164             MutableTransmutes: MutableTransmutes,
165
166             TypeAliasBounds: TypeAliasBounds,
167
168             TrivialConstraints: TrivialConstraints,
169             TypeLimits: TypeLimits::new(),
170
171             NonSnakeCase: NonSnakeCase,
172             InvalidNoMangleItems: InvalidNoMangleItems,
173
174             // Depends on access levels
175             UnreachablePub: UnreachablePub,
176
177             ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
178             InvalidValue: InvalidValue,
179         ]);
180     )
181 }
182
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]);
186     )
187 }
188
189 // FIXME: Make a separate lint type which do not require typeck tables
190 late_lint_passes!(declare_combined_late_pass, [pub BuiltinCombinedLateLintPass]);
191
192 late_lint_mod_passes!(declare_combined_late_pass, [BuiltinCombinedModuleLateLintPass]);
193
194 pub fn new_lint_store(no_interleave_lints: bool, internal_lints: bool) -> lint::LintStore {
195     let mut lint_store = lint::LintStore::new();
196
197     register_builtins(&mut lint_store, no_interleave_lints);
198     if internal_lints {
199         register_internals(&mut lint_store);
200     }
201
202     lint_store
203 }
204
205 /// Tell the `LintStore` about all the built-in lints (the ones
206 /// defined in this crate and the ones defined in
207 /// `rustc::lint::builtin`).
208 fn register_builtins(store: &mut lint::LintStore, no_interleave_lints: bool) {
209     macro_rules! add_lint_group {
210         ($name:expr, $($lint:ident),*) => (
211             store.register_group(false, $name, None, vec![$(LintId::of($lint)),*]);
212         )
213     }
214
215     macro_rules! register_pass {
216         ($method:ident, $ty:ident, $constructor:expr) => (
217             store.register_lints(&$ty::get_lints());
218             store.$method(|| box $constructor);
219         )
220     }
221
222     macro_rules! register_passes {
223         ($method:ident, [$($passes:ident: $constructor:expr,)*]) => (
224             $(
225                 register_pass!($method, $passes, $constructor);
226             )*
227         )
228     }
229
230     if no_interleave_lints {
231         pre_expansion_lint_passes!(register_passes, register_pre_expansion_pass);
232         early_lint_passes!(register_passes, register_early_pass);
233         late_lint_passes!(register_passes, register_late_pass);
234         late_lint_mod_passes!(register_passes, register_late_mod_pass);
235     } else {
236         store.register_lints(&BuiltinCombinedPreExpansionLintPass::get_lints());
237         store.register_lints(&BuiltinCombinedEarlyLintPass::get_lints());
238         store.register_lints(&BuiltinCombinedModuleLateLintPass::get_lints());
239         store.register_lints(&BuiltinCombinedLateLintPass::get_lints());
240     }
241
242     add_lint_group!("nonstandard_style",
243                     NON_CAMEL_CASE_TYPES,
244                     NON_SNAKE_CASE,
245                     NON_UPPER_CASE_GLOBALS);
246
247     add_lint_group!("unused",
248                     UNUSED_IMPORTS,
249                     UNUSED_VARIABLES,
250                     UNUSED_ASSIGNMENTS,
251                     DEAD_CODE,
252                     UNUSED_MUT,
253                     UNREACHABLE_CODE,
254                     UNREACHABLE_PATTERNS,
255                     OVERLAPPING_PATTERNS,
256                     UNUSED_MUST_USE,
257                     UNUSED_UNSAFE,
258                     PATH_STATEMENTS,
259                     UNUSED_ATTRIBUTES,
260                     UNUSED_MACROS,
261                     UNUSED_ALLOCATION,
262                     UNUSED_DOC_COMMENTS,
263                     UNUSED_EXTERN_CRATES,
264                     UNUSED_FEATURES,
265                     UNUSED_LABELS,
266                     UNUSED_PARENS);
267
268     add_lint_group!("rust_2018_idioms",
269                     BARE_TRAIT_OBJECTS,
270                     UNUSED_EXTERN_CRATES,
271                     ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
272                     ELIDED_LIFETIMES_IN_PATHS,
273                     EXPLICIT_OUTLIVES_REQUIREMENTS
274
275                     // FIXME(#52665, #47816) not always applicable and not all
276                     // macros are ready for this yet.
277                     // UNREACHABLE_PUB,
278
279                     // FIXME macro crates are not up for this yet, too much
280                     // breakage is seen if we try to encourage this lint.
281                     // MACRO_USE_EXTERN_CRATE,
282                     );
283
284     add_lint_group!("rustdoc",
285                     INTRA_DOC_LINK_RESOLUTION_FAILURE,
286                     MISSING_DOC_CODE_EXAMPLES,
287                     PRIVATE_DOC_TESTS);
288
289     // Register renamed and removed lints.
290     store.register_renamed("single_use_lifetime", "single_use_lifetimes");
291     store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
292     store.register_renamed("bare_trait_object", "bare_trait_objects");
293     store.register_renamed("unstable_name_collision", "unstable_name_collisions");
294     store.register_renamed("unused_doc_comment", "unused_doc_comments");
295     store.register_renamed("async_idents", "keyword_idents");
296     store.register_removed("unknown_features", "replaced by an error");
297     store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
298     store.register_removed("negate_unsigned", "cast a signed value instead");
299     store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
300     // Register lint group aliases.
301     store.register_group_alias("nonstandard_style", "bad_style");
302     // This was renamed to `raw_pointer_derive`, which was then removed,
303     // so it is also considered removed.
304     store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
305     store.register_removed("drop_with_repr_extern", "drop flags have been removed");
306     store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
307     store.register_removed("deprecated_attr", "use `deprecated` instead");
308     store.register_removed("transmute_from_fn_item_types",
309         "always cast functions before transmuting them");
310     store.register_removed("hr_lifetime_in_assoc_type",
311         "converted into hard error, see https://github.com/rust-lang/rust/issues/33685");
312     store.register_removed("inaccessible_extern_crate",
313         "converted into hard error, see https://github.com/rust-lang/rust/issues/36886");
314     store.register_removed("super_or_self_in_global_path",
315         "converted into hard error, see https://github.com/rust-lang/rust/issues/36888");
316     store.register_removed("overlapping_inherent_impls",
317         "converted into hard error, see https://github.com/rust-lang/rust/issues/36889");
318     store.register_removed("illegal_floating_point_constant_pattern",
319         "converted into hard error, see https://github.com/rust-lang/rust/issues/36890");
320     store.register_removed("illegal_struct_or_enum_constant_pattern",
321         "converted into hard error, see https://github.com/rust-lang/rust/issues/36891");
322     store.register_removed("lifetime_underscore",
323         "converted into hard error, see https://github.com/rust-lang/rust/issues/36892");
324     store.register_removed("extra_requirement_in_impl",
325         "converted into hard error, see https://github.com/rust-lang/rust/issues/37166");
326     store.register_removed("legacy_imports",
327         "converted into hard error, see https://github.com/rust-lang/rust/issues/38260");
328     store.register_removed("coerce_never",
329         "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
330     store.register_removed("resolve_trait_on_defaulted_unit",
331         "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
332     store.register_removed("private_no_mangle_fns",
333         "no longer a warning, `#[no_mangle]` functions always exported");
334     store.register_removed("private_no_mangle_statics",
335         "no longer a warning, `#[no_mangle]` statics always exported");
336     store.register_removed("bad_repr",
337         "replaced with a generic attribute input check");
338     store.register_removed("duplicate_matcher_binding_name",
339         "converted into hard error, see https://github.com/rust-lang/rust/issues/57742");
340     store.register_removed("incoherent_fundamental_impls",
341         "converted into hard error, see https://github.com/rust-lang/rust/issues/46205");
342     store.register_removed("legacy_constructor_visibility",
343         "converted into hard error, see https://github.com/rust-lang/rust/issues/39207");
344     store.register_removed("legacy_disrectory_ownership",
345         "converted into hard error, see https://github.com/rust-lang/rust/issues/37872");
346     store.register_removed("safe_extern_statics",
347         "converted into hard error, see https://github.com/rust-lang/rust/issues/36247");
348     store.register_removed("parenthesized_params_in_types_and_modules",
349         "converted into hard error, see https://github.com/rust-lang/rust/issues/42238");
350     store.register_removed("duplicate_macro_exports",
351         "converted into hard error, see https://github.com/rust-lang/rust/issues/35896");
352     store.register_removed("nested_impl_trait",
353         "converted into hard error, see https://github.com/rust-lang/rust/issues/59014");
354     store.register_removed("plugin_as_library", "plugins have been deprecated and retired");
355 }
356
357 fn register_internals(store: &mut lint::LintStore) {
358     store.register_lints(&DefaultHashTypes::get_lints());
359     store.register_early_pass(|| box DefaultHashTypes::new());
360     store.register_lints(&LintPassImpl::get_lints());
361     store.register_early_pass(|| box LintPassImpl);
362     store.register_lints(&TyTyKind::get_lints());
363     store.register_late_pass(|| box TyTyKind);
364     store.register_group(
365         false,
366         "rustc::internal",
367         None,
368         vec![
369             LintId::of(DEFAULT_HASH_TYPES),
370             LintId::of(USAGE_OF_TY_TYKIND),
371             LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
372             LintId::of(TY_PASS_BY_REFERENCE),
373             LintId::of(USAGE_OF_QUALIFIED_TY),
374         ],
375     );
376 }