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