]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/lib.rs
Add note to src/ci/docker/README.md about multiple docker images
[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 #[macro_use]
25 extern crate rustc_session;
26
27 mod array_into_iter;
28 mod nonstandard_style;
29 mod redundant_semicolon;
30 pub mod builtin;
31 mod types;
32 mod unused;
33 mod non_ascii_idents;
34
35 use rustc::lint;
36 use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
37 use rustc::lint::builtin::{
38     BARE_TRAIT_OBJECTS,
39     ELIDED_LIFETIMES_IN_PATHS,
40     EXPLICIT_OUTLIVES_REQUIREMENTS,
41     INTRA_DOC_LINK_RESOLUTION_FAILURE,
42     MISSING_DOC_CODE_EXAMPLES,
43     PRIVATE_DOC_TESTS,
44 };
45 use rustc::hir;
46 use rustc::hir::def_id::DefId;
47 use rustc::ty::query::Providers;
48 use rustc::ty::TyCtxt;
49
50 use syntax::ast;
51 use syntax_pos::Span;
52
53 use lint::LintId;
54
55 use redundant_semicolon::*;
56 use nonstandard_style::*;
57 use builtin::*;
58 use types::*;
59 use unused::*;
60 use non_ascii_idents::*;
61 use rustc::lint::internal::*;
62 use array_into_iter::ArrayIntoIter;
63
64 /// Useful for other parts of the compiler.
65 pub use builtin::SoftLints;
66
67 pub fn provide(providers: &mut Providers<'_>) {
68     *providers = Providers {
69         lint_mod,
70         ..*providers
71     };
72 }
73
74 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
75     lint::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, [
81             KeywordIdents: KeywordIdents,
82             UnusedDocComment: UnusedDocComment,
83         ]);
84     )
85 }
86
87 macro_rules! early_lint_passes {
88     ($macro:path, $args:tt) => (
89         $macro!($args, [
90             UnusedParens: UnusedParens,
91             UnusedImportBraces: UnusedImportBraces,
92             UnsafeCode: UnsafeCode,
93             AnonymousParameters: AnonymousParameters,
94             EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
95             NonCamelCaseTypes: NonCamelCaseTypes,
96             DeprecatedAttr: DeprecatedAttr::new(),
97             WhileTrue: WhileTrue,
98             NonAsciiIdents: NonAsciiIdents,
99             IncompleteFeatures: IncompleteFeatures,
100             RedundantSemicolon: RedundantSemicolon,
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!($args, [
117             // FIXME: Look into regression when this is used as a module lint
118             // May Depend on constants elsewhere
119             UnusedBrokenConst: UnusedBrokenConst,
120
121             // Uses attr::is_used which is untracked, can't be an incremental module pass.
122             UnusedAttributes: UnusedAttributes::new(),
123
124             // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
125             UnstableFeatures: UnstableFeatures,
126
127             // Tracks state across modules
128             UnnameableTestItems: UnnameableTestItems::new(),
129
130             // Tracks attributes of parents
131             MissingDoc: MissingDoc::new(),
132
133             // Depends on access levels
134             // FIXME: Turn the computation of types which implement Debug into a query
135             // and change this to a module lint pass
136             MissingDebugImplementations: MissingDebugImplementations::default(),
137
138             ArrayIntoIter: ArrayIntoIter,
139         ]);
140     )
141 }
142
143 macro_rules! late_lint_mod_passes {
144     ($macro:path, $args:tt) => (
145         $macro!($args, [
146             HardwiredLints: HardwiredLints,
147             ImproperCTypes: ImproperCTypes,
148             VariantSizeDifferences: VariantSizeDifferences,
149             BoxPointers: BoxPointers,
150             PathStatements: PathStatements,
151
152             // Depends on referenced function signatures in expressions
153             UnusedResults: UnusedResults,
154
155             NonUpperCaseGlobals: NonUpperCaseGlobals,
156             NonShorthandFieldPatterns: NonShorthandFieldPatterns,
157             UnusedAllocation: UnusedAllocation,
158
159             // Depends on types used in type definitions
160             MissingCopyImplementations: MissingCopyImplementations,
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     store.register_removed("plugin_as_library", "plugins have been deprecated and retired");
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 }