1 //! # Lints in the Rust compiler
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.
10 //! This API is completely unstable and subject to change.
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/")]
16 #![cfg_attr(test, feature(test))]
17 #![feature(box_patterns)]
18 #![feature(box_syntax)]
20 #![feature(rustc_diagnostic_macros)]
22 #![recursion_limit="256"]
30 extern crate rustc_target;
31 extern crate syntax_pos;
32 extern crate rustc_data_structures;
35 mod nonstandard_style;
41 use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
42 use rustc::lint::builtin::{
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,
50 parser::QUESTION_MARK_MACRO_SEP,
51 parser::ILL_FORMED_ATTRIBUTE_INPUT,
58 use syntax::edition::Edition;
63 use lint::FutureIncompatibleInfo;
65 use nonstandard_style::*;
70 /// Useful for other parts of the compiler.
71 pub use builtin::SoftLints;
73 macro_rules! pre_expansion_lint_passes {
74 ($macro:path, $args:tt) => (
76 KeywordIdents: KeywordIdents,
81 macro_rules! early_lint_passes {
82 ($macro:path, $args:tt) => (
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(),
96 macro_rules! declare_combined_early_pass {
97 ([$name:ident], $passes:tt) => (
98 early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
102 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
103 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
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)),*]);
115 macro_rules! register_passes {
116 ([$method:ident], [$($passes:ident: $constructor:expr,)*]) => (
118 store.$method(sess, false, false, box $constructor);
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]);
127 store.register_pre_expansion_pass(
131 box BuiltinCombinedPreExpansionLintPass::new()
133 store.register_early_pass(sess, false, true, box BuiltinCombinedEarlyLintPass::new());
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,
166 store.register_late_pass(sess, false, box BuiltinCombinedLateLintPass::new());
168 add_lint_group!(sess,
170 NON_CAMEL_CASE_TYPES,
172 NON_UPPER_CASE_GLOBALS);
174 add_lint_group!(sess,
182 UNREACHABLE_PATTERNS,
190 UNUSED_EXTERN_CRATES,
195 add_lint_group!(sess,
198 UNUSED_EXTERN_CRATES,
199 ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
200 ELIDED_LIFETIMES_IN_PATHS,
201 EXPLICIT_OUTLIVES_REQUIREMENTS
203 // FIXME(#52665, #47816) not always applicable and not all
204 // macros are ready for this yet.
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,
212 add_lint_group!(sess,
214 INTRA_DOC_LINK_RESOLUTION_FAILURE,
215 MISSING_DOC_CODE_EXAMPLES,
218 // Guidelines for creating a future incompatibility lint:
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>",
232 FutureIncompatibleInfo {
233 id: LintId::of(PUB_USE_OF_PRIVATE_EXTERN_CRATE),
234 reference: "issue #34537 <https://github.com/rust-lang/rust/issues/34537>",
237 FutureIncompatibleInfo {
238 id: LintId::of(PATTERNS_IN_FNS_WITHOUT_BODY),
239 reference: "issue #35203 <https://github.com/rust-lang/rust/issues/35203>",
242 FutureIncompatibleInfo {
243 id: LintId::of(DUPLICATE_MACRO_EXPORTS),
244 reference: "issue #35896 <https://github.com/rust-lang/rust/issues/35896>",
245 edition: Some(Edition::Edition2018),
247 FutureIncompatibleInfo {
248 id: LintId::of(KEYWORD_IDENTS),
249 reference: "issue #49716 <https://github.com/rust-lang/rust/issues/49716>",
250 edition: Some(Edition::Edition2018),
252 FutureIncompatibleInfo {
253 id: LintId::of(SAFE_EXTERN_STATICS),
254 reference: "issue #36247 <https://github.com/rust-lang/rust/issues/36247>",
257 FutureIncompatibleInfo {
258 id: LintId::of(INVALID_TYPE_PARAM_DEFAULT),
259 reference: "issue #36887 <https://github.com/rust-lang/rust/issues/36887>",
262 FutureIncompatibleInfo {
263 id: LintId::of(LEGACY_DIRECTORY_OWNERSHIP),
264 reference: "issue #37872 <https://github.com/rust-lang/rust/issues/37872>",
267 FutureIncompatibleInfo {
268 id: LintId::of(LEGACY_CONSTRUCTOR_VISIBILITY),
269 reference: "issue #39207 <https://github.com/rust-lang/rust/issues/39207>",
272 FutureIncompatibleInfo {
273 id: LintId::of(MISSING_FRAGMENT_SPECIFIER),
274 reference: "issue #40107 <https://github.com/rust-lang/rust/issues/40107>",
277 FutureIncompatibleInfo {
278 id: LintId::of(ILLEGAL_FLOATING_POINT_LITERAL_PATTERN),
279 reference: "issue #41620 <https://github.com/rust-lang/rust/issues/41620>",
282 FutureIncompatibleInfo {
283 id: LintId::of(ANONYMOUS_PARAMETERS),
284 reference: "issue #41686 <https://github.com/rust-lang/rust/issues/41686>",
285 edition: Some(Edition::Edition2018),
287 FutureIncompatibleInfo {
288 id: LintId::of(PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES),
289 reference: "issue #42238 <https://github.com/rust-lang/rust/issues/42238>",
292 FutureIncompatibleInfo {
293 id: LintId::of(LATE_BOUND_LIFETIME_ARGUMENTS),
294 reference: "issue #42868 <https://github.com/rust-lang/rust/issues/42868>",
297 FutureIncompatibleInfo {
298 id: LintId::of(SAFE_PACKED_BORROWS),
299 reference: "issue #46043 <https://github.com/rust-lang/rust/issues/46043>",
302 FutureIncompatibleInfo {
303 id: LintId::of(INCOHERENT_FUNDAMENTAL_IMPLS),
304 reference: "issue #46205 <https://github.com/rust-lang/rust/issues/46205>",
307 FutureIncompatibleInfo {
308 id: LintId::of(ORDER_DEPENDENT_TRAIT_OBJECTS),
309 reference: "issue #56484 <https://github.com/rust-lang/rust/issues/56484>",
312 FutureIncompatibleInfo {
313 id: LintId::of(TYVAR_BEHIND_RAW_POINTER),
314 reference: "issue #46906 <https://github.com/rust-lang/rust/issues/46906>",
315 edition: Some(Edition::Edition2018),
317 FutureIncompatibleInfo {
318 id: LintId::of(UNSTABLE_NAME_COLLISIONS),
319 reference: "issue #48919 <https://github.com/rust-lang/rust/issues/48919>",
321 // Note: this item represents future incompatibility of all unstable functions in the
322 // standard library, and thus should never be removed or changed to an error.
324 FutureIncompatibleInfo {
325 id: LintId::of(ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE),
326 reference: "issue #53130 <https://github.com/rust-lang/rust/issues/53130>",
327 edition: Some(Edition::Edition2018),
329 FutureIncompatibleInfo {
330 id: LintId::of(WHERE_CLAUSES_OBJECT_SAFETY),
331 reference: "issue #51443 <https://github.com/rust-lang/rust/issues/51443>",
334 FutureIncompatibleInfo {
335 id: LintId::of(PROC_MACRO_DERIVE_RESOLUTION_FALLBACK),
336 reference: "issue #50504 <https://github.com/rust-lang/rust/issues/50504>",
339 FutureIncompatibleInfo {
340 id: LintId::of(QUESTION_MARK_MACRO_SEP),
341 reference: "issue #48075 <https://github.com/rust-lang/rust/issues/48075>",
342 edition: Some(Edition::Edition2018),
344 FutureIncompatibleInfo {
345 id: LintId::of(MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS),
346 reference: "issue #52234 <https://github.com/rust-lang/rust/issues/52234>",
349 FutureIncompatibleInfo {
350 id: LintId::of(ILL_FORMED_ATTRIBUTE_INPUT),
351 reference: "issue #57571 <https://github.com/rust-lang/rust/issues/57571>",
354 FutureIncompatibleInfo {
355 id: LintId::of(AMBIGUOUS_ASSOCIATED_ITEMS),
356 reference: "issue #57644 <https://github.com/rust-lang/rust/issues/57644>",
361 // Register renamed and removed lints.
362 store.register_renamed("single_use_lifetime", "single_use_lifetimes");
363 store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
364 store.register_renamed("bare_trait_object", "bare_trait_objects");
365 store.register_renamed("unstable_name_collision", "unstable_name_collisions");
366 store.register_renamed("unused_doc_comment", "unused_doc_comments");
367 store.register_renamed("async_idents", "keyword_idents");
368 store.register_removed("unknown_features", "replaced by an error");
369 store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
370 store.register_removed("negate_unsigned", "cast a signed value instead");
371 store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
372 // Register lint group aliases.
373 store.register_group_alias("nonstandard_style", "bad_style");
374 // This was renamed to `raw_pointer_derive`, which was then removed,
375 // so it is also considered removed.
376 store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
377 store.register_removed("drop_with_repr_extern", "drop flags have been removed");
378 store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
379 store.register_removed("deprecated_attr", "use `deprecated` instead");
380 store.register_removed("transmute_from_fn_item_types",
381 "always cast functions before transmuting them");
382 store.register_removed("hr_lifetime_in_assoc_type",
383 "converted into hard error, see https://github.com/rust-lang/rust/issues/33685");
384 store.register_removed("inaccessible_extern_crate",
385 "converted into hard error, see https://github.com/rust-lang/rust/issues/36886");
386 store.register_removed("super_or_self_in_global_path",
387 "converted into hard error, see https://github.com/rust-lang/rust/issues/36888");
388 store.register_removed("overlapping_inherent_impls",
389 "converted into hard error, see https://github.com/rust-lang/rust/issues/36889");
390 store.register_removed("illegal_floating_point_constant_pattern",
391 "converted into hard error, see https://github.com/rust-lang/rust/issues/36890");
392 store.register_removed("illegal_struct_or_enum_constant_pattern",
393 "converted into hard error, see https://github.com/rust-lang/rust/issues/36891");
394 store.register_removed("lifetime_underscore",
395 "converted into hard error, see https://github.com/rust-lang/rust/issues/36892");
396 store.register_removed("extra_requirement_in_impl",
397 "converted into hard error, see https://github.com/rust-lang/rust/issues/37166");
398 store.register_removed("legacy_imports",
399 "converted into hard error, see https://github.com/rust-lang/rust/issues/38260");
400 store.register_removed("coerce_never",
401 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
402 store.register_removed("resolve_trait_on_defaulted_unit",
403 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
404 store.register_removed("private_no_mangle_fns",
405 "no longer a warning, #[no_mangle] functions always exported");
406 store.register_removed("private_no_mangle_statics",
407 "no longer a warning, #[no_mangle] statics always exported");
408 store.register_removed("bad_repr",
409 "replaced with a generic attribute input check");