]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/lib.rs
66e6368e83da0a116e0e21cf94831c42f65dbe78
[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_plugin` respectively.
7 //!
8 //! ## Note
9 //!
10 //! This API is completely unstable and subject to change.
11
12 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
13       html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
14       html_root_url = "https://doc.rust-lang.org/nightly/")]
15
16 #![cfg_attr(test, feature(test))]
17 #![feature(box_patterns)]
18 #![feature(box_syntax)]
19 #![feature(nll)]
20 #![feature(rustc_diagnostic_macros)]
21
22 #![recursion_limit="256"]
23
24 #[macro_use]
25 extern crate syntax;
26 #[macro_use]
27 extern crate rustc;
28 #[macro_use]
29 extern crate log;
30 extern crate rustc_target;
31 extern crate syntax_pos;
32 extern crate rustc_data_structures;
33
34 mod diagnostics;
35 mod nonstandard_style;
36 pub mod builtin;
37 mod types;
38 mod unused;
39
40 use rustc::lint;
41 use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
42 use rustc::lint::builtin::{
43     BARE_TRAIT_OBJECTS,
44     ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
45     ELIDED_LIFETIMES_IN_PATHS,
46     EXPLICIT_OUTLIVES_REQUIREMENTS,
47     INTRA_DOC_LINK_RESOLUTION_FAILURE,
48     MISSING_DOC_CODE_EXAMPLES,
49     PRIVATE_DOC_TESTS,
50     parser::QUESTION_MARK_MACRO_SEP,
51     parser::ILL_FORMED_ATTRIBUTE_INPUT,
52 };
53 use rustc::session;
54 use rustc::util;
55 use rustc::hir;
56
57 use syntax::ast;
58 use syntax::edition::Edition;
59 use syntax_pos::Span;
60
61 use session::Session;
62 use lint::LintId;
63 use lint::FutureIncompatibleInfo;
64
65 use nonstandard_style::*;
66 use builtin::*;
67 use types::*;
68 use unused::*;
69
70 /// Useful for other parts of the compiler.
71 pub use builtin::SoftLints;
72
73 macro_rules! pre_expansion_lint_passes {
74     ($macro:path, $args:tt) => (
75         $macro!($args, [
76             KeywordIdents: KeywordIdents,
77         ]);
78     )
79 }
80
81 macro_rules! early_lint_passes {
82     ($macro:path, $args:tt) => (
83         $macro!($args, [
84             UnusedParens: UnusedParens,
85             UnusedImportBraces: UnusedImportBraces,
86             UnsafeCode: UnsafeCode,
87             AnonymousParameters: AnonymousParameters,
88             UnusedDocComment: UnusedDocComment,
89             EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns,
90             NonCamelCaseTypes: NonCamelCaseTypes,
91             DeprecatedAttr: DeprecatedAttr::new(),
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 /// Tell the `LintStore` about all the built-in lints (the ones
106 /// defined in this crate and the ones defined in
107 /// `rustc::lint::builtin`).
108 pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
109     macro_rules! add_lint_group {
110         ($sess:ident, $name:expr, $($lint:ident),*) => (
111             store.register_group($sess, false, $name, None, vec![$(LintId::of($lint)),*]);
112         )
113     }
114
115     macro_rules! register_passes {
116         ([$method:ident], [$($passes:ident: $constructor:expr,)*]) => (
117             $(
118                 store.$method(sess, false, false, box $constructor);
119             )*
120         )
121     }
122
123     if sess.map(|sess| sess.opts.debugging_opts.no_interleave_lints).unwrap_or(false) {
124         pre_expansion_lint_passes!(register_passes, [register_pre_expansion_pass]);
125         early_lint_passes!(register_passes, [register_early_pass]);
126     } else {
127         store.register_pre_expansion_pass(
128             sess,
129             false,
130             true,
131             box BuiltinCombinedPreExpansionLintPass::new()
132         );
133         store.register_early_pass(sess, false, true, box BuiltinCombinedEarlyLintPass::new());
134     }
135
136     late_lint_methods!(declare_combined_late_lint_pass, [BuiltinCombinedLateLintPass, [
137         HardwiredLints: HardwiredLints,
138         WhileTrue: WhileTrue,
139         ImproperCTypes: ImproperCTypes,
140         VariantSizeDifferences: VariantSizeDifferences,
141         BoxPointers: BoxPointers,
142         UnusedAttributes: UnusedAttributes,
143         PathStatements: PathStatements,
144         UnusedResults: UnusedResults,
145         NonSnakeCase: NonSnakeCase,
146         NonUpperCaseGlobals: NonUpperCaseGlobals,
147         NonShorthandFieldPatterns: NonShorthandFieldPatterns,
148         UnusedAllocation: UnusedAllocation,
149         MissingCopyImplementations: MissingCopyImplementations,
150         UnstableFeatures: UnstableFeatures,
151         InvalidNoMangleItems: InvalidNoMangleItems,
152         PluginAsLibrary: PluginAsLibrary,
153         MutableTransmutes: MutableTransmutes,
154         UnionsWithDropFields: UnionsWithDropFields,
155         UnreachablePub: UnreachablePub,
156         UnnameableTestItems: UnnameableTestItems::new(),
157         TypeAliasBounds: TypeAliasBounds,
158         UnusedBrokenConst: UnusedBrokenConst,
159         TrivialConstraints: TrivialConstraints,
160         TypeLimits: TypeLimits::new(),
161         MissingDoc: MissingDoc::new(),
162         MissingDebugImplementations: MissingDebugImplementations::new(),
163         ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
164     ]], ['tcx]);
165
166     store.register_late_pass(sess, false, box BuiltinCombinedLateLintPass::new());
167
168     add_lint_group!(sess,
169                     "nonstandard_style",
170                     NON_CAMEL_CASE_TYPES,
171                     NON_SNAKE_CASE,
172                     NON_UPPER_CASE_GLOBALS);
173
174     add_lint_group!(sess,
175                     "unused",
176                     UNUSED_IMPORTS,
177                     UNUSED_VARIABLES,
178                     UNUSED_ASSIGNMENTS,
179                     DEAD_CODE,
180                     UNUSED_MUT,
181                     UNREACHABLE_CODE,
182                     UNREACHABLE_PATTERNS,
183                     UNUSED_MUST_USE,
184                     UNUSED_UNSAFE,
185                     PATH_STATEMENTS,
186                     UNUSED_ATTRIBUTES,
187                     UNUSED_MACROS,
188                     UNUSED_ALLOCATION,
189                     UNUSED_DOC_COMMENTS,
190                     UNUSED_EXTERN_CRATES,
191                     UNUSED_FEATURES,
192                     UNUSED_LABELS,
193                     UNUSED_PARENS);
194
195     add_lint_group!(sess,
196                     "rust_2018_idioms",
197                     BARE_TRAIT_OBJECTS,
198                     UNUSED_EXTERN_CRATES,
199                     ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
200                     ELIDED_LIFETIMES_IN_PATHS,
201                     EXPLICIT_OUTLIVES_REQUIREMENTS
202
203                     // FIXME(#52665, #47816) not always applicable and not all
204                     // macros are ready for this yet.
205                     // UNREACHABLE_PUB,
206
207                     // FIXME macro crates are not up for this yet, too much
208                     // breakage is seen if we try to encourage this lint.
209                     // MACRO_USE_EXTERN_CRATE,
210                     );
211
212     add_lint_group!(sess,
213                     "rustdoc",
214                     INTRA_DOC_LINK_RESOLUTION_FAILURE,
215                     MISSING_DOC_CODE_EXAMPLES,
216                     PRIVATE_DOC_TESTS);
217
218     // Guidelines for creating a future incompatibility lint:
219     //
220     // - Create a lint defaulting to warn as normal, with ideally the same error
221     //   message you would normally give
222     // - Add a suitable reference, typically an RFC or tracking issue. Go ahead
223     //   and include the full URL, sort items in ascending order of issue numbers.
224     // - Later, change lint to error
225     // - Eventually, remove lint
226     store.register_future_incompatible(sess, vec![
227         FutureIncompatibleInfo {
228             id: LintId::of(PRIVATE_IN_PUBLIC),
229             reference: "issue #34537 <https://github.com/rust-lang/rust/issues/34537>",
230             edition: None,
231         },
232         FutureIncompatibleInfo {
233             id: LintId::of(EXTERNAL_PRIVATE_DEPENDENCY),
234             reference: "issue #44663 <https://github.com/rust-lang/rust/issues/44663>",
235             edition: None,
236         },
237         FutureIncompatibleInfo {
238             id: LintId::of(PUB_USE_OF_PRIVATE_EXTERN_CRATE),
239             reference: "issue #34537 <https://github.com/rust-lang/rust/issues/34537>",
240             edition: None,
241         },
242         FutureIncompatibleInfo {
243             id: LintId::of(PATTERNS_IN_FNS_WITHOUT_BODY),
244             reference: "issue #35203 <https://github.com/rust-lang/rust/issues/35203>",
245             edition: None,
246         },
247         FutureIncompatibleInfo {
248             id: LintId::of(DUPLICATE_MACRO_EXPORTS),
249             reference: "issue #35896 <https://github.com/rust-lang/rust/issues/35896>",
250             edition: Some(Edition::Edition2018),
251         },
252         FutureIncompatibleInfo {
253             id: LintId::of(KEYWORD_IDENTS),
254             reference: "issue #49716 <https://github.com/rust-lang/rust/issues/49716>",
255             edition: Some(Edition::Edition2018),
256         },
257         FutureIncompatibleInfo {
258             id: LintId::of(SAFE_EXTERN_STATICS),
259             reference: "issue #36247 <https://github.com/rust-lang/rust/issues/36247>",
260             edition: None,
261         },
262         FutureIncompatibleInfo {
263             id: LintId::of(INVALID_TYPE_PARAM_DEFAULT),
264             reference: "issue #36887 <https://github.com/rust-lang/rust/issues/36887>",
265             edition: None,
266         },
267         FutureIncompatibleInfo {
268             id: LintId::of(LEGACY_DIRECTORY_OWNERSHIP),
269             reference: "issue #37872 <https://github.com/rust-lang/rust/issues/37872>",
270             edition: None,
271         },
272         FutureIncompatibleInfo {
273             id: LintId::of(LEGACY_CONSTRUCTOR_VISIBILITY),
274             reference: "issue #39207 <https://github.com/rust-lang/rust/issues/39207>",
275             edition: None,
276         },
277         FutureIncompatibleInfo {
278             id: LintId::of(MISSING_FRAGMENT_SPECIFIER),
279             reference: "issue #40107 <https://github.com/rust-lang/rust/issues/40107>",
280             edition: None,
281         },
282         FutureIncompatibleInfo {
283             id: LintId::of(ILLEGAL_FLOATING_POINT_LITERAL_PATTERN),
284             reference: "issue #41620 <https://github.com/rust-lang/rust/issues/41620>",
285             edition: None,
286         },
287         FutureIncompatibleInfo {
288             id: LintId::of(ANONYMOUS_PARAMETERS),
289             reference: "issue #41686 <https://github.com/rust-lang/rust/issues/41686>",
290             edition: Some(Edition::Edition2018),
291         },
292         FutureIncompatibleInfo {
293             id: LintId::of(PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES),
294             reference: "issue #42238 <https://github.com/rust-lang/rust/issues/42238>",
295             edition: None,
296         },
297         FutureIncompatibleInfo {
298             id: LintId::of(LATE_BOUND_LIFETIME_ARGUMENTS),
299             reference: "issue #42868 <https://github.com/rust-lang/rust/issues/42868>",
300             edition: None,
301         },
302         FutureIncompatibleInfo {
303             id: LintId::of(SAFE_PACKED_BORROWS),
304             reference: "issue #46043 <https://github.com/rust-lang/rust/issues/46043>",
305             edition: None,
306         },
307         FutureIncompatibleInfo {
308             id: LintId::of(INCOHERENT_FUNDAMENTAL_IMPLS),
309             reference: "issue #46205 <https://github.com/rust-lang/rust/issues/46205>",
310             edition: None,
311         },
312         FutureIncompatibleInfo {
313             id: LintId::of(ORDER_DEPENDENT_TRAIT_OBJECTS),
314             reference: "issue #56484 <https://github.com/rust-lang/rust/issues/56484>",
315             edition: None,
316         },
317         FutureIncompatibleInfo {
318             id: LintId::of(TYVAR_BEHIND_RAW_POINTER),
319             reference: "issue #46906 <https://github.com/rust-lang/rust/issues/46906>",
320             edition: Some(Edition::Edition2018),
321         },
322         FutureIncompatibleInfo {
323             id: LintId::of(UNSTABLE_NAME_COLLISIONS),
324             reference: "issue #48919 <https://github.com/rust-lang/rust/issues/48919>",
325             edition: None,
326             // Note: this item represents future incompatibility of all unstable functions in the
327             //       standard library, and thus should never be removed or changed to an error.
328         },
329         FutureIncompatibleInfo {
330             id: LintId::of(ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE),
331             reference: "issue #53130 <https://github.com/rust-lang/rust/issues/53130>",
332             edition: Some(Edition::Edition2018),
333         },
334         FutureIncompatibleInfo {
335             id: LintId::of(WHERE_CLAUSES_OBJECT_SAFETY),
336             reference: "issue #51443 <https://github.com/rust-lang/rust/issues/51443>",
337             edition: None,
338         },
339         FutureIncompatibleInfo {
340             id: LintId::of(PROC_MACRO_DERIVE_RESOLUTION_FALLBACK),
341             reference: "issue #50504 <https://github.com/rust-lang/rust/issues/50504>",
342             edition: None,
343         },
344         FutureIncompatibleInfo {
345             id: LintId::of(QUESTION_MARK_MACRO_SEP),
346             reference: "issue #48075 <https://github.com/rust-lang/rust/issues/48075>",
347             edition: Some(Edition::Edition2018),
348         },
349         FutureIncompatibleInfo {
350             id: LintId::of(MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS),
351             reference: "issue #52234 <https://github.com/rust-lang/rust/issues/52234>",
352             edition: None,
353         },
354         FutureIncompatibleInfo {
355             id: LintId::of(ILL_FORMED_ATTRIBUTE_INPUT),
356             reference: "issue #57571 <https://github.com/rust-lang/rust/issues/57571>",
357             edition: None,
358         },
359         FutureIncompatibleInfo {
360             id: LintId::of(AMBIGUOUS_ASSOCIATED_ITEMS),
361             reference: "issue #57644 <https://github.com/rust-lang/rust/issues/57644>",
362             edition: None,
363         },
364         ]);
365
366     // Register renamed and removed lints.
367     store.register_renamed("single_use_lifetime", "single_use_lifetimes");
368     store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
369     store.register_renamed("bare_trait_object", "bare_trait_objects");
370     store.register_renamed("unstable_name_collision", "unstable_name_collisions");
371     store.register_renamed("unused_doc_comment", "unused_doc_comments");
372     store.register_renamed("async_idents", "keyword_idents");
373     store.register_removed("unknown_features", "replaced by an error");
374     store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
375     store.register_removed("negate_unsigned", "cast a signed value instead");
376     store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
377     // Register lint group aliases.
378     store.register_group_alias("nonstandard_style", "bad_style");
379     // This was renamed to `raw_pointer_derive`, which was then removed,
380     // so it is also considered removed.
381     store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
382     store.register_removed("drop_with_repr_extern", "drop flags have been removed");
383     store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
384     store.register_removed("deprecated_attr", "use `deprecated` instead");
385     store.register_removed("transmute_from_fn_item_types",
386         "always cast functions before transmuting them");
387     store.register_removed("hr_lifetime_in_assoc_type",
388         "converted into hard error, see https://github.com/rust-lang/rust/issues/33685");
389     store.register_removed("inaccessible_extern_crate",
390         "converted into hard error, see https://github.com/rust-lang/rust/issues/36886");
391     store.register_removed("super_or_self_in_global_path",
392         "converted into hard error, see https://github.com/rust-lang/rust/issues/36888");
393     store.register_removed("overlapping_inherent_impls",
394         "converted into hard error, see https://github.com/rust-lang/rust/issues/36889");
395     store.register_removed("illegal_floating_point_constant_pattern",
396         "converted into hard error, see https://github.com/rust-lang/rust/issues/36890");
397     store.register_removed("illegal_struct_or_enum_constant_pattern",
398         "converted into hard error, see https://github.com/rust-lang/rust/issues/36891");
399     store.register_removed("lifetime_underscore",
400         "converted into hard error, see https://github.com/rust-lang/rust/issues/36892");
401     store.register_removed("extra_requirement_in_impl",
402         "converted into hard error, see https://github.com/rust-lang/rust/issues/37166");
403     store.register_removed("legacy_imports",
404         "converted into hard error, see https://github.com/rust-lang/rust/issues/38260");
405     store.register_removed("coerce_never",
406         "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
407     store.register_removed("resolve_trait_on_defaulted_unit",
408         "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
409     store.register_removed("private_no_mangle_fns",
410         "no longer a warning, #[no_mangle] functions always exported");
411     store.register_removed("private_no_mangle_statics",
412         "no longer a warning, #[no_mangle] statics always exported");
413     store.register_removed("bad_repr",
414         "replaced with a generic attribute input check");
415 }