]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/lib.rs
Deprecate using rustc_plugin without the rustc_driver dylib.
[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(rustc_diagnostic_macros)]
19
20 #![recursion_limit="256"]
21
22 #[macro_use]
23 extern crate rustc;
24
25 mod error_codes;
26 mod nonstandard_style;
27 mod redundant_semicolon;
28 pub mod builtin;
29 mod types;
30 mod unused;
31 mod non_ascii_idents;
32
33 use rustc::lint;
34 use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
35 use rustc::lint::builtin::{
36     BARE_TRAIT_OBJECTS,
37     ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
38     ELIDED_LIFETIMES_IN_PATHS,
39     EXPLICIT_OUTLIVES_REQUIREMENTS,
40     INTRA_DOC_LINK_RESOLUTION_FAILURE,
41     MISSING_DOC_CODE_EXAMPLES,
42     PRIVATE_DOC_TESTS,
43     parser::ILL_FORMED_ATTRIBUTE_INPUT,
44 };
45 use rustc::session;
46 use rustc::hir;
47 use rustc::hir::def_id::DefId;
48 use rustc::ty::query::Providers;
49 use rustc::ty::TyCtxt;
50
51 use syntax::ast;
52 use syntax::edition::Edition;
53 use syntax_pos::Span;
54
55 use session::Session;
56 use lint::LintId;
57 use lint::FutureIncompatibleInfo;
58
59 use redundant_semicolon::*;
60 use nonstandard_style::*;
61 use builtin::*;
62 use types::*;
63 use unused::*;
64 use non_ascii_idents::*;
65 use rustc::lint::internal::*;
66
67 /// Useful for other parts of the compiler.
68 pub use builtin::SoftLints;
69
70 pub fn provide(providers: &mut Providers<'_>) {
71     *providers = Providers {
72         lint_mod,
73         ..*providers
74     };
75 }
76
77 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
78     lint::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
79 }
80
81 macro_rules! pre_expansion_lint_passes {
82     ($macro:path, $args:tt) => (
83         $macro!($args, [
84             KeywordIdents: KeywordIdents,
85             UnusedDocComment: UnusedDocComment,
86         ]);
87     )
88 }
89
90 macro_rules! early_lint_passes {
91     ($macro:path, $args:tt) => (
92         $macro!($args, [
93             UnusedParens: UnusedParens,
94             UnusedImportBraces: UnusedImportBraces,
95             UnsafeCode: UnsafeCode,
96             AnonymousParameters: AnonymousParameters,
97             EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
98             NonCamelCaseTypes: NonCamelCaseTypes,
99             DeprecatedAttr: DeprecatedAttr::new(),
100             WhileTrue: WhileTrue,
101             NonAsciiIdents: NonAsciiIdents,
102             IncompleteFeatures: IncompleteFeatures,
103             RedundantSemicolon: RedundantSemicolon,
104         ]);
105     )
106 }
107
108 macro_rules! declare_combined_early_pass {
109     ([$name:ident], $passes:tt) => (
110         early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
111     )
112 }
113
114 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
115 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
116
117 macro_rules! late_lint_passes {
118     ($macro:path, $args:tt) => (
119         $macro!($args, [
120             // FIXME: Look into regression when this is used as a module lint
121             // May Depend on constants elsewhere
122             UnusedBrokenConst: UnusedBrokenConst,
123
124             // Uses attr::is_used which is untracked, can't be an incremental module pass.
125             UnusedAttributes: UnusedAttributes::new(),
126
127             // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
128             UnstableFeatures: UnstableFeatures,
129
130             // Tracks state across modules
131             UnnameableTestItems: UnnameableTestItems::new(),
132
133             // Tracks attributes of parents
134             MissingDoc: MissingDoc::new(),
135
136             // Depends on access levels
137             // FIXME: Turn the computation of types which implement Debug into a query
138             // and change this to a module lint pass
139             MissingDebugImplementations: MissingDebugImplementations::default(),
140         ]);
141     )
142 }
143
144 macro_rules! late_lint_mod_passes {
145     ($macro:path, $args:tt) => (
146         $macro!($args, [
147             HardwiredLints: HardwiredLints,
148             ImproperCTypes: ImproperCTypes,
149             VariantSizeDifferences: VariantSizeDifferences,
150             BoxPointers: BoxPointers,
151             PathStatements: PathStatements,
152
153             // Depends on referenced function signatures in expressions
154             UnusedResults: UnusedResults,
155
156             NonUpperCaseGlobals: NonUpperCaseGlobals,
157             NonShorthandFieldPatterns: NonShorthandFieldPatterns,
158             UnusedAllocation: UnusedAllocation,
159
160             // Depends on types used in type definitions
161             MissingCopyImplementations: MissingCopyImplementations,
162
163             PluginAsLibrary: PluginAsLibrary,
164
165             // Depends on referenced function signatures in expressions
166             MutableTransmutes: MutableTransmutes,
167
168             // Depends on types of fields, checks if they implement Drop
169             UnionsWithDropFields: UnionsWithDropFields,
170
171             TypeAliasBounds: TypeAliasBounds,
172
173             TrivialConstraints: TrivialConstraints,
174             TypeLimits: TypeLimits::new(),
175
176             NonSnakeCase: NonSnakeCase,
177             InvalidNoMangleItems: InvalidNoMangleItems,
178
179             // Depends on access levels
180             UnreachablePub: UnreachablePub,
181
182             ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
183             InvalidValue: InvalidValue,
184         ]);
185     )
186 }
187
188 macro_rules! declare_combined_late_pass {
189     ([$v:vis $name:ident], $passes:tt) => (
190         late_lint_methods!(declare_combined_late_lint_pass, [$v $name, $passes], ['tcx]);
191     )
192 }
193
194 // FIXME: Make a separate lint type which do not require typeck tables
195 late_lint_passes!(declare_combined_late_pass, [pub BuiltinCombinedLateLintPass]);
196
197 late_lint_mod_passes!(declare_combined_late_pass, [BuiltinCombinedModuleLateLintPass]);
198
199 /// Tell the `LintStore` about all the built-in lints (the ones
200 /// defined in this crate and the ones defined in
201 /// `rustc::lint::builtin`).
202 pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
203     macro_rules! add_lint_group {
204         ($sess:ident, $name:expr, $($lint:ident),*) => (
205             store.register_group($sess, false, $name, None, vec![$(LintId::of($lint)),*]);
206         )
207     }
208
209     macro_rules! register_pass {
210         ($method:ident, $constructor:expr, [$($args:expr),*]) => (
211             store.$method(sess, false, false, $($args,)* box $constructor);
212         )
213     }
214
215     macro_rules! register_passes {
216         ([$method:ident, $args:tt], [$($passes:ident: $constructor:expr,)*]) => (
217             $(
218                 register_pass!($method, $constructor, $args);
219             )*
220         )
221     }
222
223     if sess.map(|sess| sess.opts.debugging_opts.no_interleave_lints).unwrap_or(false) {
224         pre_expansion_lint_passes!(register_passes, [register_pre_expansion_pass, []]);
225         early_lint_passes!(register_passes, [register_early_pass, []]);
226         late_lint_passes!(register_passes, [register_late_pass, [false]]);
227         late_lint_mod_passes!(register_passes, [register_late_pass, [true]]);
228     } else {
229         store.register_pre_expansion_pass(
230             sess,
231             false,
232             true,
233             box BuiltinCombinedPreExpansionLintPass::new()
234         );
235         store.register_early_pass(sess, false, true, box BuiltinCombinedEarlyLintPass::new());
236         store.register_late_pass(
237             sess, false, true, true, box BuiltinCombinedModuleLateLintPass::new()
238         );
239         store.register_late_pass(
240             sess, false, true, false, box BuiltinCombinedLateLintPass::new()
241         );
242     }
243
244     add_lint_group!(sess,
245                     "nonstandard_style",
246                     NON_CAMEL_CASE_TYPES,
247                     NON_SNAKE_CASE,
248                     NON_UPPER_CASE_GLOBALS);
249
250     add_lint_group!(sess,
251                     "unused",
252                     UNUSED_IMPORTS,
253                     UNUSED_VARIABLES,
254                     UNUSED_ASSIGNMENTS,
255                     DEAD_CODE,
256                     UNUSED_MUT,
257                     UNREACHABLE_CODE,
258                     UNREACHABLE_PATTERNS,
259                     UNUSED_MUST_USE,
260                     UNUSED_UNSAFE,
261                     PATH_STATEMENTS,
262                     UNUSED_ATTRIBUTES,
263                     UNUSED_MACROS,
264                     UNUSED_ALLOCATION,
265                     UNUSED_DOC_COMMENTS,
266                     UNUSED_EXTERN_CRATES,
267                     UNUSED_FEATURES,
268                     UNUSED_LABELS,
269                     UNUSED_PARENS);
270
271     add_lint_group!(sess,
272                     "rust_2018_idioms",
273                     BARE_TRAIT_OBJECTS,
274                     UNUSED_EXTERN_CRATES,
275                     ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
276                     ELIDED_LIFETIMES_IN_PATHS,
277                     EXPLICIT_OUTLIVES_REQUIREMENTS
278
279                     // FIXME(#52665, #47816) not always applicable and not all
280                     // macros are ready for this yet.
281                     // UNREACHABLE_PUB,
282
283                     // FIXME macro crates are not up for this yet, too much
284                     // breakage is seen if we try to encourage this lint.
285                     // MACRO_USE_EXTERN_CRATE,
286                     );
287
288     add_lint_group!(sess,
289                     "rustdoc",
290                     INTRA_DOC_LINK_RESOLUTION_FAILURE,
291                     MISSING_DOC_CODE_EXAMPLES,
292                     PRIVATE_DOC_TESTS);
293
294     // Guidelines for creating a future incompatibility lint:
295     //
296     // - Create a lint defaulting to warn as normal, with ideally the same error
297     //   message you would normally give
298     // - Add a suitable reference, typically an RFC or tracking issue. Go ahead
299     //   and include the full URL, sort items in ascending order of issue numbers.
300     // - Later, change lint to error
301     // - Eventually, remove lint
302     store.register_future_incompatible(sess, vec![
303         FutureIncompatibleInfo {
304             id: LintId::of(PRIVATE_IN_PUBLIC),
305             reference: "issue #34537 <https://github.com/rust-lang/rust/issues/34537>",
306             edition: None,
307         },
308         FutureIncompatibleInfo {
309             id: LintId::of(PUB_USE_OF_PRIVATE_EXTERN_CRATE),
310             reference: "issue #34537 <https://github.com/rust-lang/rust/issues/34537>",
311             edition: None,
312         },
313         FutureIncompatibleInfo {
314             id: LintId::of(PATTERNS_IN_FNS_WITHOUT_BODY),
315             reference: "issue #35203 <https://github.com/rust-lang/rust/issues/35203>",
316             edition: None,
317         },
318         FutureIncompatibleInfo {
319             id: LintId::of(DUPLICATE_MACRO_EXPORTS),
320             reference: "issue #35896 <https://github.com/rust-lang/rust/issues/35896>",
321             edition: Some(Edition::Edition2018),
322         },
323         FutureIncompatibleInfo {
324             id: LintId::of(KEYWORD_IDENTS),
325             reference: "issue #49716 <https://github.com/rust-lang/rust/issues/49716>",
326             edition: Some(Edition::Edition2018),
327         },
328         FutureIncompatibleInfo {
329             id: LintId::of(SAFE_EXTERN_STATICS),
330             reference: "issue #36247 <https://github.com/rust-lang/rust/issues/36247>",
331             edition: None,
332         },
333         FutureIncompatibleInfo {
334             id: LintId::of(INVALID_TYPE_PARAM_DEFAULT),
335             reference: "issue #36887 <https://github.com/rust-lang/rust/issues/36887>",
336             edition: None,
337         },
338         FutureIncompatibleInfo {
339             id: LintId::of(LEGACY_DIRECTORY_OWNERSHIP),
340             reference: "issue #37872 <https://github.com/rust-lang/rust/issues/37872>",
341             edition: None,
342         },
343         FutureIncompatibleInfo {
344             id: LintId::of(LEGACY_CONSTRUCTOR_VISIBILITY),
345             reference: "issue #39207 <https://github.com/rust-lang/rust/issues/39207>",
346             edition: None,
347         },
348         FutureIncompatibleInfo {
349             id: LintId::of(MISSING_FRAGMENT_SPECIFIER),
350             reference: "issue #40107 <https://github.com/rust-lang/rust/issues/40107>",
351             edition: None,
352         },
353         FutureIncompatibleInfo {
354             id: LintId::of(ILLEGAL_FLOATING_POINT_LITERAL_PATTERN),
355             reference: "issue #41620 <https://github.com/rust-lang/rust/issues/41620>",
356             edition: None,
357         },
358         FutureIncompatibleInfo {
359             id: LintId::of(ANONYMOUS_PARAMETERS),
360             reference: "issue #41686 <https://github.com/rust-lang/rust/issues/41686>",
361             edition: Some(Edition::Edition2018),
362         },
363         FutureIncompatibleInfo {
364             id: LintId::of(PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES),
365             reference: "issue #42238 <https://github.com/rust-lang/rust/issues/42238>",
366             edition: None,
367         },
368         FutureIncompatibleInfo {
369             id: LintId::of(LATE_BOUND_LIFETIME_ARGUMENTS),
370             reference: "issue #42868 <https://github.com/rust-lang/rust/issues/42868>",
371             edition: None,
372         },
373         FutureIncompatibleInfo {
374             id: LintId::of(SAFE_PACKED_BORROWS),
375             reference: "issue #46043 <https://github.com/rust-lang/rust/issues/46043>",
376             edition: None,
377         },
378         FutureIncompatibleInfo {
379             id: LintId::of(ORDER_DEPENDENT_TRAIT_OBJECTS),
380             reference: "issue #56484 <https://github.com/rust-lang/rust/issues/56484>",
381             edition: None,
382         },
383         FutureIncompatibleInfo {
384             id: LintId::of(TYVAR_BEHIND_RAW_POINTER),
385             reference: "issue #46906 <https://github.com/rust-lang/rust/issues/46906>",
386             edition: Some(Edition::Edition2018),
387         },
388         FutureIncompatibleInfo {
389             id: LintId::of(UNSTABLE_NAME_COLLISIONS),
390             reference: "issue #48919 <https://github.com/rust-lang/rust/issues/48919>",
391             edition: None,
392             // Note: this item represents future incompatibility of all unstable functions in the
393             //       standard library, and thus should never be removed or changed to an error.
394         },
395         FutureIncompatibleInfo {
396             id: LintId::of(ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE),
397             reference: "issue #53130 <https://github.com/rust-lang/rust/issues/53130>",
398             edition: Some(Edition::Edition2018),
399         },
400         FutureIncompatibleInfo {
401             id: LintId::of(WHERE_CLAUSES_OBJECT_SAFETY),
402             reference: "issue #51443 <https://github.com/rust-lang/rust/issues/51443>",
403             edition: None,
404         },
405         FutureIncompatibleInfo {
406             id: LintId::of(PROC_MACRO_DERIVE_RESOLUTION_FALLBACK),
407             reference: "issue #50504 <https://github.com/rust-lang/rust/issues/50504>",
408             edition: None,
409         },
410         FutureIncompatibleInfo {
411             id: LintId::of(MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS),
412             reference: "issue #52234 <https://github.com/rust-lang/rust/issues/52234>",
413             edition: None,
414         },
415         FutureIncompatibleInfo {
416             id: LintId::of(ILL_FORMED_ATTRIBUTE_INPUT),
417             reference: "issue #57571 <https://github.com/rust-lang/rust/issues/57571>",
418             edition: None,
419         },
420         FutureIncompatibleInfo {
421             id: LintId::of(AMBIGUOUS_ASSOCIATED_ITEMS),
422             reference: "issue #57644 <https://github.com/rust-lang/rust/issues/57644>",
423             edition: None,
424         },
425         FutureIncompatibleInfo {
426             id: LintId::of(NESTED_IMPL_TRAIT),
427             reference: "issue #59014 <https://github.com/rust-lang/rust/issues/59014>",
428             edition: None,
429         },
430         FutureIncompatibleInfo {
431             id: LintId::of(MUTABLE_BORROW_RESERVATION_CONFLICT),
432             reference: "issue #59159 <https://github.com/rust-lang/rust/issues/59159>",
433             edition: None,
434         },
435         FutureIncompatibleInfo {
436             id: LintId::of(INDIRECT_STRUCTURAL_MATCH),
437             reference: "issue #62411 <https://github.com/rust-lang/rust/issues/62411>",
438             edition: None,
439         }
440         ]);
441
442     // Register renamed and removed lints.
443     store.register_renamed("single_use_lifetime", "single_use_lifetimes");
444     store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
445     store.register_renamed("bare_trait_object", "bare_trait_objects");
446     store.register_renamed("unstable_name_collision", "unstable_name_collisions");
447     store.register_renamed("unused_doc_comment", "unused_doc_comments");
448     store.register_renamed("async_idents", "keyword_idents");
449     store.register_removed("unknown_features", "replaced by an error");
450     store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
451     store.register_removed("negate_unsigned", "cast a signed value instead");
452     store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
453     // Register lint group aliases.
454     store.register_group_alias("nonstandard_style", "bad_style");
455     // This was renamed to `raw_pointer_derive`, which was then removed,
456     // so it is also considered removed.
457     store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
458     store.register_removed("drop_with_repr_extern", "drop flags have been removed");
459     store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
460     store.register_removed("deprecated_attr", "use `deprecated` instead");
461     store.register_removed("transmute_from_fn_item_types",
462         "always cast functions before transmuting them");
463     store.register_removed("hr_lifetime_in_assoc_type",
464         "converted into hard error, see https://github.com/rust-lang/rust/issues/33685");
465     store.register_removed("inaccessible_extern_crate",
466         "converted into hard error, see https://github.com/rust-lang/rust/issues/36886");
467     store.register_removed("super_or_self_in_global_path",
468         "converted into hard error, see https://github.com/rust-lang/rust/issues/36888");
469     store.register_removed("overlapping_inherent_impls",
470         "converted into hard error, see https://github.com/rust-lang/rust/issues/36889");
471     store.register_removed("illegal_floating_point_constant_pattern",
472         "converted into hard error, see https://github.com/rust-lang/rust/issues/36890");
473     store.register_removed("illegal_struct_or_enum_constant_pattern",
474         "converted into hard error, see https://github.com/rust-lang/rust/issues/36891");
475     store.register_removed("lifetime_underscore",
476         "converted into hard error, see https://github.com/rust-lang/rust/issues/36892");
477     store.register_removed("extra_requirement_in_impl",
478         "converted into hard error, see https://github.com/rust-lang/rust/issues/37166");
479     store.register_removed("legacy_imports",
480         "converted into hard error, see https://github.com/rust-lang/rust/issues/38260");
481     store.register_removed("coerce_never",
482         "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
483     store.register_removed("resolve_trait_on_defaulted_unit",
484         "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
485     store.register_removed("private_no_mangle_fns",
486         "no longer a warning, `#[no_mangle]` functions always exported");
487     store.register_removed("private_no_mangle_statics",
488         "no longer a warning, `#[no_mangle]` statics always exported");
489     store.register_removed("bad_repr",
490         "replaced with a generic attribute input check");
491     store.register_removed("duplicate_matcher_binding_name",
492         "converted into hard error, see https://github.com/rust-lang/rust/issues/57742");
493     store.register_removed("incoherent_fundamental_impls",
494         "converted into hard error, see https://github.com/rust-lang/rust/issues/46205");
495 }
496
497 pub fn register_internals(store: &mut lint::LintStore, sess: Option<&Session>) {
498     store.register_early_pass(sess, false, false, box DefaultHashTypes::new());
499     store.register_early_pass(sess, false, false, box LintPassImpl);
500     store.register_late_pass(sess, false, false, false, box TyTyKind);
501     store.register_group(
502         sess,
503         false,
504         "rustc::internal",
505         None,
506         vec![
507             LintId::of(DEFAULT_HASH_TYPES),
508             LintId::of(USAGE_OF_TY_TYKIND),
509             LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
510             LintId::of(TY_PASS_BY_REFERENCE),
511             LintId::of(USAGE_OF_QUALIFIED_TY),
512         ],
513     );
514 }