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