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