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