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