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