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