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