]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/lib.rs
Auto merge of #66225 - Centril:rollup-it0t5tk, 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
14 #![cfg_attr(test, feature(test))]
15 #![feature(box_patterns)]
16 #![feature(box_syntax)]
17 #![feature(nll)]
18 #![feature(matches_macro)]
19
20 #![recursion_limit="256"]
21
22 #[macro_use]
23 extern crate rustc;
24
25 mod array_into_iter;
26 mod error_codes;
27 mod nonstandard_style;
28 mod redundant_semicolon;
29 pub mod builtin;
30 mod types;
31 mod unused;
32 mod non_ascii_idents;
33
34 use rustc::lint;
35 use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
36 use rustc::lint::builtin::{
37     BARE_TRAIT_OBJECTS,
38     ELIDED_LIFETIMES_IN_PATHS,
39     EXPLICIT_OUTLIVES_REQUIREMENTS,
40     INTRA_DOC_LINK_RESOLUTION_FAILURE,
41     MISSING_DOC_CODE_EXAMPLES,
42     PRIVATE_DOC_TESTS,
43 };
44 use rustc::hir;
45 use rustc::hir::def_id::DefId;
46 use rustc::ty::query::Providers;
47 use rustc::ty::TyCtxt;
48
49 use syntax::ast;
50 use syntax_pos::Span;
51
52 use lint::LintId;
53
54 use redundant_semicolon::*;
55 use nonstandard_style::*;
56 use builtin::*;
57 use types::*;
58 use unused::*;
59 use non_ascii_idents::*;
60 use rustc::lint::internal::*;
61 use array_into_iter::ArrayIntoIter;
62
63 /// Useful for other parts of the compiler.
64 pub use builtin::SoftLints;
65
66 pub fn provide(providers: &mut Providers<'_>) {
67     *providers = Providers {
68         lint_mod,
69         ..*providers
70     };
71 }
72
73 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
74     lint::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
75 }
76
77 macro_rules! pre_expansion_lint_passes {
78     ($macro:path, $args:tt) => (
79         $macro!($args, [
80             KeywordIdents: KeywordIdents,
81             UnusedDocComment: UnusedDocComment,
82         ]);
83     )
84 }
85
86 macro_rules! early_lint_passes {
87     ($macro:path, $args:tt) => (
88         $macro!($args, [
89             UnusedParens: UnusedParens,
90             UnusedImportBraces: UnusedImportBraces,
91             UnsafeCode: UnsafeCode,
92             AnonymousParameters: AnonymousParameters,
93             EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
94             NonCamelCaseTypes: NonCamelCaseTypes,
95             DeprecatedAttr: DeprecatedAttr::new(),
96             WhileTrue: WhileTrue,
97             NonAsciiIdents: NonAsciiIdents,
98             IncompleteFeatures: IncompleteFeatures,
99             RedundantSemicolon: RedundantSemicolon,
100         ]);
101     )
102 }
103
104 macro_rules! declare_combined_early_pass {
105     ([$name:ident], $passes:tt) => (
106         early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
107     )
108 }
109
110 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
111 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
112
113 macro_rules! late_lint_passes {
114     ($macro:path, $args:tt) => (
115         $macro!($args, [
116             // FIXME: Look into regression when this is used as a module lint
117             // May Depend on constants elsewhere
118             UnusedBrokenConst: UnusedBrokenConst,
119
120             // Uses attr::is_used which is untracked, can't be an incremental module pass.
121             UnusedAttributes: UnusedAttributes::new(),
122
123             // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
124             UnstableFeatures: UnstableFeatures,
125
126             // Tracks state across modules
127             UnnameableTestItems: UnnameableTestItems::new(),
128
129             // Tracks attributes of parents
130             MissingDoc: MissingDoc::new(),
131
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(),
136
137             ArrayIntoIter: ArrayIntoIter,
138         ]);
139     )
140 }
141
142 macro_rules! late_lint_mod_passes {
143     ($macro:path, $args:tt) => (
144         $macro!($args, [
145             HardwiredLints: HardwiredLints,
146             ImproperCTypes: ImproperCTypes,
147             VariantSizeDifferences: VariantSizeDifferences,
148             BoxPointers: BoxPointers,
149             PathStatements: PathStatements,
150
151             // Depends on referenced function signatures in expressions
152             UnusedResults: UnusedResults,
153
154             NonUpperCaseGlobals: NonUpperCaseGlobals,
155             NonShorthandFieldPatterns: NonShorthandFieldPatterns,
156             UnusedAllocation: UnusedAllocation,
157
158             // Depends on types used in type definitions
159             MissingCopyImplementations: MissingCopyImplementations,
160
161             PluginAsLibrary: PluginAsLibrary,
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 }
355
356 fn register_internals(store: &mut lint::LintStore) {
357     store.register_lints(&DefaultHashTypes::get_lints());
358     store.register_early_pass(|| box DefaultHashTypes::new());
359     store.register_lints(&LintPassImpl::get_lints());
360     store.register_early_pass(|| box LintPassImpl);
361     store.register_lints(&TyTyKind::get_lints());
362     store.register_late_pass(|| box TyTyKind);
363     store.register_group(
364         false,
365         "rustc::internal",
366         None,
367         vec![
368             LintId::of(DEFAULT_HASH_TYPES),
369             LintId::of(USAGE_OF_TY_TYKIND),
370             LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
371             LintId::of(TY_PASS_BY_REFERENCE),
372             LintId::of(USAGE_OF_QUALIFIED_TY),
373         ],
374     );
375 }