rustc_lint_defs/builtin.rs
1//! Some lints that are built in to the compiler.
2//!
3//! These are the built-in lints that are emitted direct in the main
4//! compiler code, rather than using their own custom pass. Those
5//! lints are all available in `rustc_lint::builtin`.
6//!
7//! When removing a lint, make sure to also add a call to `register_removed` in
8//! compiler/rustc_lint/src/lib.rs.
9
10use rustc_span::edition::Edition;
11
12use crate::{FutureIncompatibilityReason, declare_lint, declare_lint_pass};
13
14declare_lint_pass! {
15 /// Does nothing as a lint pass, but registers some `Lint`s
16 /// that are used by other parts of the compiler.
17 HardwiredLints => [
18 // tidy-alphabetical-start
19 AARCH64_SOFTFLOAT_NEON,
20 ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
21 AMBIGUOUS_ASSOCIATED_ITEMS,
22 AMBIGUOUS_GLOB_IMPORTS,
23 AMBIGUOUS_GLOB_REEXPORTS,
24 ARITHMETIC_OVERFLOW,
25 ASM_SUB_REGISTER,
26 BAD_ASM_STYLE,
27 BARE_TRAIT_OBJECTS,
28 BINDINGS_WITH_VARIANT_NAME,
29 BREAK_WITH_LABEL_AND_LOOP,
30 COHERENCE_LEAK_CHECK,
31 CONFLICTING_REPR_HINTS,
32 CONST_EVALUATABLE_UNCHECKED,
33 CONST_ITEM_MUTATION,
34 DEAD_CODE,
35 DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK,
36 DEPRECATED,
37 DEPRECATED_IN_FUTURE,
38 DEPRECATED_SAFE_2024,
39 DEPRECATED_WHERE_CLAUSE_LOCATION,
40 DUPLICATE_MACRO_ATTRIBUTES,
41 ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
42 ELIDED_LIFETIMES_IN_PATHS,
43 EXPLICIT_BUILTIN_CFGS_IN_FLAGS,
44 EXPORTED_PRIVATE_DEPENDENCIES,
45 FFI_UNWIND_CALLS,
46 FORBIDDEN_LINT_GROUPS,
47 FUNCTION_ITEM_REFERENCES,
48 FUZZY_PROVENANCE_CASTS,
49 HIDDEN_GLOB_REEXPORTS,
50 ILL_FORMED_ATTRIBUTE_INPUT,
51 INCOMPLETE_INCLUDE,
52 INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
53 INLINE_NO_SANITIZE,
54 INVALID_DOC_ATTRIBUTES,
55 INVALID_MACRO_EXPORT_ARGUMENTS,
56 INVALID_TYPE_PARAM_DEFAULT,
57 IRREFUTABLE_LET_PATTERNS,
58 LARGE_ASSIGNMENTS,
59 LATE_BOUND_LIFETIME_ARGUMENTS,
60 LEGACY_DERIVE_HELPERS,
61 LINKER_MESSAGES,
62 LONG_RUNNING_CONST_EVAL,
63 LOSSY_PROVENANCE_CASTS,
64 MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
65 MACRO_USE_EXTERN_CRATE,
66 META_VARIABLE_MISUSE,
67 MISSING_ABI,
68 MISSING_UNSAFE_ON_EXTERN,
69 MUST_NOT_SUSPEND,
70 NAMED_ARGUMENTS_USED_POSITIONALLY,
71 NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
72 NON_CONTIGUOUS_RANGE_ENDPOINTS,
73 NON_EXHAUSTIVE_OMITTED_PATTERNS,
74 OUT_OF_SCOPE_MACRO_CALLS,
75 OVERLAPPING_RANGE_ENDPOINTS,
76 PATTERNS_IN_FNS_WITHOUT_BODY,
77 PRIVATE_BOUNDS,
78 PRIVATE_INTERFACES,
79 PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
80 PUB_USE_OF_PRIVATE_EXTERN_CRATE,
81 REDUNDANT_IMPORTS,
82 REDUNDANT_LIFETIMES,
83 REFINING_IMPL_TRAIT_INTERNAL,
84 REFINING_IMPL_TRAIT_REACHABLE,
85 RENAMED_AND_REMOVED_LINTS,
86 REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
87 RUST_2021_INCOMPATIBLE_CLOSURE_CAPTURES,
88 RUST_2021_INCOMPATIBLE_OR_PATTERNS,
89 RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX,
90 RUST_2021_PRELUDE_COLLISIONS,
91 RUST_2024_GUARDED_STRING_INCOMPATIBLE_SYNTAX,
92 RUST_2024_INCOMPATIBLE_PAT,
93 RUST_2024_PRELUDE_COLLISIONS,
94 SELF_CONSTRUCTOR_FROM_OUTER_ITEM,
95 SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
96 SINGLE_USE_LIFETIMES,
97 SOFT_UNSTABLE,
98 STABLE_FEATURES,
99 SUPERTRAIT_ITEM_SHADOWING_DEFINITION,
100 SUPERTRAIT_ITEM_SHADOWING_USAGE,
101 TAIL_EXPR_DROP_ORDER,
102 TEST_UNSTABLE_LINT,
103 TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
104 TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
105 TRIVIAL_CASTS,
106 TRIVIAL_NUMERIC_CASTS,
107 TYVAR_BEHIND_RAW_POINTER,
108 UNCONDITIONAL_PANIC,
109 UNCONDITIONAL_RECURSION,
110 UNCOVERED_PARAM_IN_PROJECTION,
111 UNEXPECTED_CFGS,
112 UNFULFILLED_LINT_EXPECTATIONS,
113 UNINHABITED_STATIC,
114 UNKNOWN_CRATE_TYPES,
115 UNKNOWN_LINTS,
116 UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
117 UNNAMEABLE_TEST_ITEMS,
118 UNNAMEABLE_TYPES,
119 UNREACHABLE_CODE,
120 UNREACHABLE_PATTERNS,
121 UNSAFE_ATTR_OUTSIDE_UNSAFE,
122 UNSAFE_OP_IN_UNSAFE_FN,
123 UNSTABLE_NAME_COLLISIONS,
124 UNSTABLE_SYNTAX_PRE_EXPANSION,
125 UNSUPPORTED_CALLING_CONVENTIONS,
126 UNSUPPORTED_FN_PTR_CALLING_CONVENTIONS,
127 UNUSED_ASSIGNMENTS,
128 UNUSED_ASSOCIATED_TYPE_BOUNDS,
129 UNUSED_ATTRIBUTES,
130 UNUSED_CRATE_DEPENDENCIES,
131 UNUSED_EXTERN_CRATES,
132 UNUSED_FEATURES,
133 UNUSED_IMPORTS,
134 UNUSED_LABELS,
135 UNUSED_LIFETIMES,
136 UNUSED_MACRO_RULES,
137 UNUSED_MACROS,
138 UNUSED_MUT,
139 UNUSED_QUALIFICATIONS,
140 UNUSED_UNSAFE,
141 UNUSED_VARIABLES,
142 USELESS_DEPRECATED,
143 WARNINGS,
144 // tidy-alphabetical-end
145 ]
146}
147
148declare_lint! {
149 /// The `forbidden_lint_groups` lint detects violations of
150 /// `forbid` applied to a lint group. Due to a bug in the compiler,
151 /// these used to be overlooked entirely. They now generate a warning.
152 ///
153 /// ### Example
154 ///
155 /// ```rust
156 /// #![forbid(warnings)]
157 /// #![warn(bad_style)]
158 ///
159 /// fn main() {}
160 /// ```
161 ///
162 /// {{produces}}
163 ///
164 /// ### Recommended fix
165 ///
166 /// If your crate is using `#![forbid(warnings)]`,
167 /// we recommend that you change to `#![deny(warnings)]`.
168 ///
169 /// ### Explanation
170 ///
171 /// Due to a compiler bug, applying `forbid` to lint groups
172 /// previously had no effect. The bug is now fixed but instead of
173 /// enforcing `forbid` we issue this future-compatibility warning
174 /// to avoid breaking existing crates.
175 pub FORBIDDEN_LINT_GROUPS,
176 Warn,
177 "applying forbid to lint-groups",
178 @future_incompatible = FutureIncompatibleInfo {
179 reason: FutureIncompatibilityReason::FutureReleaseError,
180 reference: "issue #81670 <https://github.com/rust-lang/rust/issues/81670>",
181 report_in_deps: true,
182 };
183}
184
185declare_lint! {
186 /// The `ill_formed_attribute_input` lint detects ill-formed attribute
187 /// inputs that were previously accepted and used in practice.
188 ///
189 /// ### Example
190 ///
191 /// ```rust,compile_fail
192 /// #[inline = "this is not valid"]
193 /// fn foo() {}
194 /// ```
195 ///
196 /// {{produces}}
197 ///
198 /// ### Explanation
199 ///
200 /// Previously, inputs for many built-in attributes weren't validated and
201 /// nonsensical attribute inputs were accepted. After validation was
202 /// added, it was determined that some existing projects made use of these
203 /// invalid forms. This is a [future-incompatible] lint to transition this
204 /// to a hard error in the future. See [issue #57571] for more details.
205 ///
206 /// Check the [attribute reference] for details on the valid inputs for
207 /// attributes.
208 ///
209 /// [issue #57571]: https://github.com/rust-lang/rust/issues/57571
210 /// [attribute reference]: https://doc.rust-lang.org/nightly/reference/attributes.html
211 /// [future-incompatible]: ../index.md#future-incompatible-lints
212 pub ILL_FORMED_ATTRIBUTE_INPUT,
213 Deny,
214 "ill-formed attribute inputs that were previously accepted and used in practice",
215 @future_incompatible = FutureIncompatibleInfo {
216 reason: FutureIncompatibilityReason::FutureReleaseError,
217 reference: "issue #57571 <https://github.com/rust-lang/rust/issues/57571>",
218 };
219 crate_level_only
220}
221
222declare_lint! {
223 /// The `conflicting_repr_hints` lint detects [`repr` attributes] with
224 /// conflicting hints.
225 ///
226 /// [`repr` attributes]: https://doc.rust-lang.org/reference/type-layout.html#representations
227 ///
228 /// ### Example
229 ///
230 /// ```rust,compile_fail
231 /// #[repr(u32, u64)]
232 /// enum Foo {
233 /// Variant1,
234 /// }
235 /// ```
236 ///
237 /// {{produces}}
238 ///
239 /// ### Explanation
240 ///
241 /// The compiler incorrectly accepted these conflicting representations in
242 /// the past. This is a [future-incompatible] lint to transition this to a
243 /// hard error in the future. See [issue #68585] for more details.
244 ///
245 /// To correct the issue, remove one of the conflicting hints.
246 ///
247 /// [issue #68585]: https://github.com/rust-lang/rust/issues/68585
248 /// [future-incompatible]: ../index.md#future-incompatible-lints
249 pub CONFLICTING_REPR_HINTS,
250 Deny,
251 "conflicts between `#[repr(..)]` hints that were previously accepted and used in practice",
252 @future_incompatible = FutureIncompatibleInfo {
253 reason: FutureIncompatibilityReason::FutureReleaseError,
254 reference: "issue #68585 <https://github.com/rust-lang/rust/issues/68585>",
255 report_in_deps: true,
256 };
257}
258
259declare_lint! {
260 /// The `meta_variable_misuse` lint detects possible meta-variable misuse
261 /// in macro definitions.
262 ///
263 /// ### Example
264 ///
265 /// ```rust,compile_fail
266 /// #![deny(meta_variable_misuse)]
267 ///
268 /// macro_rules! foo {
269 /// () => {};
270 /// ($( $i:ident = $($j:ident),+ );*) => { $( $( $i = $k; )+ )* };
271 /// }
272 ///
273 /// fn main() {
274 /// foo!();
275 /// }
276 /// ```
277 ///
278 /// {{produces}}
279 ///
280 /// ### Explanation
281 ///
282 /// There are quite a few different ways a [`macro_rules`] macro can be
283 /// improperly defined. Many of these errors were previously only detected
284 /// when the macro was expanded or not at all. This lint is an attempt to
285 /// catch some of these problems when the macro is *defined*.
286 ///
287 /// This lint is "allow" by default because it may have false positives
288 /// and other issues. See [issue #61053] for more details.
289 ///
290 /// [`macro_rules`]: https://doc.rust-lang.org/reference/macros-by-example.html
291 /// [issue #61053]: https://github.com/rust-lang/rust/issues/61053
292 pub META_VARIABLE_MISUSE,
293 Allow,
294 "possible meta-variable misuse at macro definition"
295}
296
297declare_lint! {
298 /// The `incomplete_include` lint detects the use of the [`include!`]
299 /// macro with a file that contains more than one expression.
300 ///
301 /// [`include!`]: https://doc.rust-lang.org/std/macro.include.html
302 ///
303 /// ### Example
304 ///
305 /// ```rust,ignore (needs separate file)
306 /// fn main() {
307 /// include!("foo.txt");
308 /// }
309 /// ```
310 ///
311 /// where the file `foo.txt` contains:
312 ///
313 /// ```text
314 /// println!("hi!");
315 /// ```
316 ///
317 /// produces:
318 ///
319 /// ```text
320 /// error: include macro expected single expression in source
321 /// --> foo.txt:1:14
322 /// |
323 /// 1 | println!("1");
324 /// | ^
325 /// |
326 /// = note: `#[deny(incomplete_include)]` on by default
327 /// ```
328 ///
329 /// ### Explanation
330 ///
331 /// The [`include!`] macro is currently only intended to be used to
332 /// include a single [expression] or multiple [items]. Historically it
333 /// would ignore any contents after the first expression, but that can be
334 /// confusing. In the example above, the `println!` expression ends just
335 /// before the semicolon, making the semicolon "extra" information that is
336 /// ignored. Perhaps even more surprising, if the included file had
337 /// multiple print statements, the subsequent ones would be ignored!
338 ///
339 /// One workaround is to place the contents in braces to create a [block
340 /// expression]. Also consider alternatives, like using functions to
341 /// encapsulate the expressions, or use [proc-macros].
342 ///
343 /// This is a lint instead of a hard error because existing projects were
344 /// found to hit this error. To be cautious, it is a lint for now. The
345 /// future semantics of the `include!` macro are also uncertain, see
346 /// [issue #35560].
347 ///
348 /// [items]: https://doc.rust-lang.org/reference/items.html
349 /// [expression]: https://doc.rust-lang.org/reference/expressions.html
350 /// [block expression]: https://doc.rust-lang.org/reference/expressions/block-expr.html
351 /// [proc-macros]: https://doc.rust-lang.org/reference/procedural-macros.html
352 /// [issue #35560]: https://github.com/rust-lang/rust/issues/35560
353 pub INCOMPLETE_INCLUDE,
354 Deny,
355 "trailing content in included file"
356}
357
358declare_lint! {
359 /// The `arithmetic_overflow` lint detects that an arithmetic operation
360 /// will [overflow].
361 ///
362 /// [overflow]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow
363 ///
364 /// ### Example
365 ///
366 /// ```rust,compile_fail
367 /// 1_i32 << 32;
368 /// ```
369 ///
370 /// {{produces}}
371 ///
372 /// ### Explanation
373 ///
374 /// It is very likely a mistake to perform an arithmetic operation that
375 /// overflows its value. If the compiler is able to detect these kinds of
376 /// overflows at compile-time, it will trigger this lint. Consider
377 /// adjusting the expression to avoid overflow, or use a data type that
378 /// will not overflow.
379 pub ARITHMETIC_OVERFLOW,
380 Deny,
381 "arithmetic operation overflows",
382 @eval_always = true
383}
384
385declare_lint! {
386 /// The `unconditional_panic` lint detects an operation that will cause a
387 /// panic at runtime.
388 ///
389 /// ### Example
390 ///
391 /// ```rust,compile_fail
392 /// # #![allow(unused)]
393 /// let x = 1 / 0;
394 /// ```
395 ///
396 /// {{produces}}
397 ///
398 /// ### Explanation
399 ///
400 /// This lint detects code that is very likely incorrect because it will
401 /// always panic, such as division by zero and out-of-bounds array
402 /// accesses. Consider adjusting your code if this is a bug, or using the
403 /// `panic!` or `unreachable!` macro instead in case the panic is intended.
404 pub UNCONDITIONAL_PANIC,
405 Deny,
406 "operation will cause a panic at runtime",
407 @eval_always = true
408}
409
410declare_lint! {
411 /// The `unused_imports` lint detects imports that are never used.
412 ///
413 /// ### Example
414 ///
415 /// ```rust
416 /// use std::collections::HashMap;
417 /// ```
418 ///
419 /// {{produces}}
420 ///
421 /// ### Explanation
422 ///
423 /// Unused imports may signal a mistake or unfinished code, and clutter
424 /// the code, and should be removed. If you intended to re-export the item
425 /// to make it available outside of the module, add a visibility modifier
426 /// like `pub`.
427 pub UNUSED_IMPORTS,
428 Warn,
429 "imports that are never used"
430}
431
432declare_lint! {
433 /// The `redundant_imports` lint detects imports that are redundant due to being
434 /// imported already; either through a previous import, or being present in
435 /// the prelude.
436 ///
437 /// ### Example
438 ///
439 /// ```rust,compile_fail
440 /// #![deny(redundant_imports)]
441 /// use std::option::Option::None;
442 /// fn foo() -> Option<i32> { None }
443 /// ```
444 ///
445 /// {{produces}}
446 ///
447 /// ### Explanation
448 ///
449 /// Redundant imports are unnecessary and can be removed to simplify code.
450 /// If you intended to re-export the item to make it available outside of the
451 /// module, add a visibility modifier like `pub`.
452 pub REDUNDANT_IMPORTS,
453 Allow,
454 "imports that are redundant due to being imported already"
455}
456
457declare_lint! {
458 /// The `must_not_suspend` lint guards against values that shouldn't be held across suspend points
459 /// (`.await`)
460 ///
461 /// ### Example
462 ///
463 /// ```rust
464 /// #![feature(must_not_suspend)]
465 /// #![warn(must_not_suspend)]
466 ///
467 /// #[must_not_suspend]
468 /// struct SyncThing {}
469 ///
470 /// async fn yield_now() {}
471 ///
472 /// pub async fn uhoh() {
473 /// let guard = SyncThing {};
474 /// yield_now().await;
475 /// let _guard = guard;
476 /// }
477 /// ```
478 ///
479 /// {{produces}}
480 ///
481 /// ### Explanation
482 ///
483 /// The `must_not_suspend` lint detects values that are marked with the `#[must_not_suspend]`
484 /// attribute being held across suspend points. A "suspend" point is usually a `.await` in an async
485 /// function.
486 ///
487 /// This attribute can be used to mark values that are semantically incorrect across suspends
488 /// (like certain types of timers), values that have async alternatives, and values that
489 /// regularly cause problems with the `Send`-ness of async fn's returned futures (like
490 /// `MutexGuard`'s)
491 ///
492 pub MUST_NOT_SUSPEND,
493 Allow,
494 "use of a `#[must_not_suspend]` value across a yield point",
495 @feature_gate = must_not_suspend;
496}
497
498declare_lint! {
499 /// The `unused_extern_crates` lint guards against `extern crate` items
500 /// that are never used.
501 ///
502 /// ### Example
503 ///
504 /// ```rust,compile_fail
505 /// #![deny(unused_extern_crates)]
506 /// #![deny(warnings)]
507 /// extern crate proc_macro;
508 /// ```
509 ///
510 /// {{produces}}
511 ///
512 /// ### Explanation
513 ///
514 /// `extern crate` items that are unused have no effect and should be
515 /// removed. Note that there are some cases where specifying an `extern
516 /// crate` is desired for the side effect of ensuring the given crate is
517 /// linked, even though it is not otherwise directly referenced. The lint
518 /// can be silenced by aliasing the crate to an underscore, such as
519 /// `extern crate foo as _`. Also note that it is no longer idiomatic to
520 /// use `extern crate` in the [2018 edition], as extern crates are now
521 /// automatically added in scope.
522 ///
523 /// This lint is "allow" by default because it can be noisy, and produce
524 /// false-positives. If a dependency is being removed from a project, it
525 /// is recommended to remove it from the build configuration (such as
526 /// `Cargo.toml`) to ensure stale build entries aren't left behind.
527 ///
528 /// [2018 edition]: https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-clarity.html#no-more-extern-crate
529 pub UNUSED_EXTERN_CRATES,
530 Allow,
531 "extern crates that are never used"
532}
533
534declare_lint! {
535 /// The `unused_crate_dependencies` lint detects crate dependencies that
536 /// are never used.
537 ///
538 /// ### Example
539 ///
540 /// ```rust,ignore (needs extern crate)
541 /// #![deny(unused_crate_dependencies)]
542 /// ```
543 ///
544 /// This will produce:
545 ///
546 /// ```text
547 /// error: extern crate `regex` is unused in crate `lint_example`
548 /// |
549 /// = help: remove the dependency or add `use regex as _;` to the crate root
550 /// note: the lint level is defined here
551 /// --> src/lib.rs:1:9
552 /// |
553 /// 1 | #![deny(unused_crate_dependencies)]
554 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^
555 /// ```
556 ///
557 /// ### Explanation
558 ///
559 /// After removing the code that uses a dependency, this usually also
560 /// requires removing the dependency from the build configuration.
561 /// However, sometimes that step can be missed, which leads to time wasted
562 /// building dependencies that are no longer used. This lint can be
563 /// enabled to detect dependencies that are never used (more specifically,
564 /// any dependency passed with the `--extern` command-line flag that is
565 /// never referenced via [`use`], [`extern crate`], or in any [path]).
566 ///
567 /// This lint is "allow" by default because it can provide false positives
568 /// depending on how the build system is configured. For example, when
569 /// using Cargo, a "package" consists of multiple crates (such as a
570 /// library and a binary), but the dependencies are defined for the
571 /// package as a whole. If there is a dependency that is only used in the
572 /// binary, but not the library, then the lint will be incorrectly issued
573 /// in the library.
574 ///
575 /// [path]: https://doc.rust-lang.org/reference/paths.html
576 /// [`use`]: https://doc.rust-lang.org/reference/items/use-declarations.html
577 /// [`extern crate`]: https://doc.rust-lang.org/reference/items/extern-crates.html
578 pub UNUSED_CRATE_DEPENDENCIES,
579 Allow,
580 "crate dependencies that are never used",
581 crate_level_only
582}
583
584declare_lint! {
585 /// The `unused_qualifications` lint detects unnecessarily qualified
586 /// names.
587 ///
588 /// ### Example
589 ///
590 /// ```rust,compile_fail
591 /// #![deny(unused_qualifications)]
592 /// mod foo {
593 /// pub fn bar() {}
594 /// }
595 ///
596 /// fn main() {
597 /// use foo::bar;
598 /// foo::bar();
599 /// bar();
600 /// }
601 /// ```
602 ///
603 /// {{produces}}
604 ///
605 /// ### Explanation
606 ///
607 /// If an item from another module is already brought into scope, then
608 /// there is no need to qualify it in this case. You can call `bar()`
609 /// directly, without the `foo::`.
610 ///
611 /// This lint is "allow" by default because it is somewhat pedantic, and
612 /// doesn't indicate an actual problem, but rather a stylistic choice, and
613 /// can be noisy when refactoring or moving around code.
614 pub UNUSED_QUALIFICATIONS,
615 Allow,
616 "detects unnecessarily qualified names"
617}
618
619declare_lint! {
620 /// The `unknown_lints` lint detects unrecognized lint attributes.
621 ///
622 /// ### Example
623 ///
624 /// ```rust
625 /// #![allow(not_a_real_lint)]
626 /// ```
627 ///
628 /// {{produces}}
629 ///
630 /// ### Explanation
631 ///
632 /// It is usually a mistake to specify a lint that does not exist. Check
633 /// the spelling, and check the lint listing for the correct name. Also
634 /// consider if you are using an old version of the compiler, and the lint
635 /// is only available in a newer version.
636 pub UNKNOWN_LINTS,
637 Warn,
638 "unrecognized lint attribute",
639 @eval_always = true
640}
641
642declare_lint! {
643 /// The `unfulfilled_lint_expectations` lint detects when a lint expectation is
644 /// unfulfilled.
645 ///
646 /// ### Example
647 ///
648 /// ```rust
649 /// #[expect(unused_variables)]
650 /// let x = 10;
651 /// println!("{}", x);
652 /// ```
653 ///
654 /// {{produces}}
655 ///
656 /// ### Explanation
657 ///
658 /// The `#[expect]` attribute can be used to create a lint expectation. The
659 /// expectation is fulfilled, if a `#[warn]` attribute at the same location
660 /// would result in a lint emission. If the expectation is unfulfilled,
661 /// because no lint was emitted, this lint will be emitted on the attribute.
662 ///
663 pub UNFULFILLED_LINT_EXPECTATIONS,
664 Warn,
665 "unfulfilled lint expectation"
666}
667
668declare_lint! {
669 /// The `unused_variables` lint detects variables which are not used in
670 /// any way.
671 ///
672 /// ### Example
673 ///
674 /// ```rust
675 /// let x = 5;
676 /// ```
677 ///
678 /// {{produces}}
679 ///
680 /// ### Explanation
681 ///
682 /// Unused variables may signal a mistake or unfinished code. To silence
683 /// the warning for the individual variable, prefix it with an underscore
684 /// such as `_x`.
685 pub UNUSED_VARIABLES,
686 Warn,
687 "detect variables which are not used in any way"
688}
689
690declare_lint! {
691 /// The `unused_assignments` lint detects assignments that will never be read.
692 ///
693 /// ### Example
694 ///
695 /// ```rust
696 /// let mut x = 5;
697 /// x = 6;
698 /// ```
699 ///
700 /// {{produces}}
701 ///
702 /// ### Explanation
703 ///
704 /// Unused assignments may signal a mistake or unfinished code. If the
705 /// variable is never used after being assigned, then the assignment can
706 /// be removed. Variables with an underscore prefix such as `_x` will not
707 /// trigger this lint.
708 pub UNUSED_ASSIGNMENTS,
709 Warn,
710 "detect assignments that will never be read"
711}
712
713declare_lint! {
714 /// The `dead_code` lint detects unused, unexported items.
715 ///
716 /// ### Example
717 ///
718 /// ```rust
719 /// fn foo() {}
720 /// ```
721 ///
722 /// {{produces}}
723 ///
724 /// ### Explanation
725 ///
726 /// Dead code may signal a mistake or unfinished code. To silence the
727 /// warning for individual items, prefix the name with an underscore such
728 /// as `_foo`. If it was intended to expose the item outside of the crate,
729 /// consider adding a visibility modifier like `pub`.
730 ///
731 /// To preserve the numbering of tuple structs with unused fields,
732 /// change the unused fields to have unit type or use
733 /// `PhantomData`.
734 ///
735 /// Otherwise consider removing the unused code.
736 ///
737 /// ### Limitations
738 ///
739 /// Removing fields that are only used for side-effects and never
740 /// read will result in behavioral changes. Examples of this
741 /// include:
742 ///
743 /// - If a field's value performs an action when it is dropped.
744 /// - If a field's type does not implement an auto trait
745 /// (e.g. `Send`, `Sync`, `Unpin`).
746 ///
747 /// For side-effects from dropping field values, this lint should
748 /// be allowed on those fields. For side-effects from containing
749 /// field types, `PhantomData` should be used.
750 pub DEAD_CODE,
751 Warn,
752 "detect unused, unexported items"
753}
754
755declare_lint! {
756 /// The `unused_attributes` lint detects attributes that were not used by
757 /// the compiler.
758 ///
759 /// ### Example
760 ///
761 /// ```rust
762 /// #![ignore]
763 /// ```
764 ///
765 /// {{produces}}
766 ///
767 /// ### Explanation
768 ///
769 /// Unused [attributes] may indicate the attribute is placed in the wrong
770 /// position. Consider removing it, or placing it in the correct position.
771 /// Also consider if you intended to use an _inner attribute_ (with a `!`
772 /// such as `#![allow(unused)]`) which applies to the item the attribute
773 /// is within, or an _outer attribute_ (without a `!` such as
774 /// `#[allow(unused)]`) which applies to the item *following* the
775 /// attribute.
776 ///
777 /// [attributes]: https://doc.rust-lang.org/reference/attributes.html
778 pub UNUSED_ATTRIBUTES,
779 Warn,
780 "detects attributes that were not used by the compiler"
781}
782
783declare_lint! {
784 /// The `unreachable_code` lint detects unreachable code paths.
785 ///
786 /// ### Example
787 ///
788 /// ```rust,no_run
789 /// panic!("we never go past here!");
790 ///
791 /// let x = 5;
792 /// ```
793 ///
794 /// {{produces}}
795 ///
796 /// ### Explanation
797 ///
798 /// Unreachable code may signal a mistake or unfinished code. If the code
799 /// is no longer in use, consider removing it.
800 pub UNREACHABLE_CODE,
801 Warn,
802 "detects unreachable code paths",
803 report_in_external_macro
804}
805
806declare_lint! {
807 /// The `unreachable_patterns` lint detects unreachable patterns.
808 ///
809 /// ### Example
810 ///
811 /// ```rust
812 /// let x = 5;
813 /// match x {
814 /// y => (),
815 /// 5 => (),
816 /// }
817 /// ```
818 ///
819 /// {{produces}}
820 ///
821 /// ### Explanation
822 ///
823 /// This usually indicates a mistake in how the patterns are specified or
824 /// ordered. In this example, the `y` pattern will always match, so the
825 /// five is impossible to reach. Remember, match arms match in order, you
826 /// probably wanted to put the `5` case above the `y` case.
827 pub UNREACHABLE_PATTERNS,
828 Warn,
829 "detects unreachable patterns"
830}
831
832declare_lint! {
833 /// The `overlapping_range_endpoints` lint detects `match` arms that have [range patterns] that
834 /// overlap on their endpoints.
835 ///
836 /// [range patterns]: https://doc.rust-lang.org/nightly/reference/patterns.html#range-patterns
837 ///
838 /// ### Example
839 ///
840 /// ```rust
841 /// let x = 123u8;
842 /// match x {
843 /// 0..=100 => { println!("small"); }
844 /// 100..=255 => { println!("large"); }
845 /// }
846 /// ```
847 ///
848 /// {{produces}}
849 ///
850 /// ### Explanation
851 ///
852 /// It is likely a mistake to have range patterns in a match expression that overlap in this
853 /// way. Check that the beginning and end values are what you expect, and keep in mind that
854 /// with `..=` the left and right bounds are inclusive.
855 pub OVERLAPPING_RANGE_ENDPOINTS,
856 Warn,
857 "detects range patterns with overlapping endpoints"
858}
859
860declare_lint! {
861 /// The `non_contiguous_range_endpoints` lint detects likely off-by-one errors when using
862 /// exclusive [range patterns].
863 ///
864 /// [range patterns]: https://doc.rust-lang.org/nightly/reference/patterns.html#range-patterns
865 ///
866 /// ### Example
867 ///
868 /// ```rust
869 /// let x = 123u32;
870 /// match x {
871 /// 0..100 => { println!("small"); }
872 /// 101..1000 => { println!("large"); }
873 /// _ => { println!("larger"); }
874 /// }
875 /// ```
876 ///
877 /// {{produces}}
878 ///
879 /// ### Explanation
880 ///
881 /// It is likely a mistake to have range patterns in a match expression that miss out a single
882 /// number. Check that the beginning and end values are what you expect, and keep in mind that
883 /// with `..=` the right bound is inclusive, and with `..` it is exclusive.
884 pub NON_CONTIGUOUS_RANGE_ENDPOINTS,
885 Warn,
886 "detects off-by-one errors with exclusive range patterns"
887}
888
889declare_lint! {
890 /// The `bindings_with_variant_name` lint detects pattern bindings with
891 /// the same name as one of the matched variants.
892 ///
893 /// ### Example
894 ///
895 /// ```rust,compile_fail
896 /// pub enum Enum {
897 /// Foo,
898 /// Bar,
899 /// }
900 ///
901 /// pub fn foo(x: Enum) {
902 /// match x {
903 /// Foo => {}
904 /// Bar => {}
905 /// }
906 /// }
907 /// ```
908 ///
909 /// {{produces}}
910 ///
911 /// ### Explanation
912 ///
913 /// It is usually a mistake to specify an enum variant name as an
914 /// [identifier pattern]. In the example above, the `match` arms are
915 /// specifying a variable name to bind the value of `x` to. The second arm
916 /// is ignored because the first one matches *all* values. The likely
917 /// intent is that the arm was intended to match on the enum variant.
918 ///
919 /// Two possible solutions are:
920 ///
921 /// * Specify the enum variant using a [path pattern], such as
922 /// `Enum::Foo`.
923 /// * Bring the enum variants into local scope, such as adding `use
924 /// Enum::*;` to the beginning of the `foo` function in the example
925 /// above.
926 ///
927 /// [identifier pattern]: https://doc.rust-lang.org/reference/patterns.html#identifier-patterns
928 /// [path pattern]: https://doc.rust-lang.org/reference/patterns.html#path-patterns
929 pub BINDINGS_WITH_VARIANT_NAME,
930 Deny,
931 "detects pattern bindings with the same name as one of the matched variants"
932}
933
934declare_lint! {
935 /// The `unused_macros` lint detects macros that were not used.
936 ///
937 /// Note that this lint is distinct from the `unused_macro_rules` lint,
938 /// which checks for single rules that never match of an otherwise used
939 /// macro, and thus never expand.
940 ///
941 /// ### Example
942 ///
943 /// ```rust
944 /// macro_rules! unused {
945 /// () => {};
946 /// }
947 ///
948 /// fn main() {
949 /// }
950 /// ```
951 ///
952 /// {{produces}}
953 ///
954 /// ### Explanation
955 ///
956 /// Unused macros may signal a mistake or unfinished code. To silence the
957 /// warning for the individual macro, prefix the name with an underscore
958 /// such as `_my_macro`. If you intended to export the macro to make it
959 /// available outside of the crate, use the [`macro_export` attribute].
960 ///
961 /// [`macro_export` attribute]: https://doc.rust-lang.org/reference/macros-by-example.html#path-based-scope
962 pub UNUSED_MACROS,
963 Warn,
964 "detects macros that were not used"
965}
966
967declare_lint! {
968 /// The `unused_macro_rules` lint detects macro rules that were not used.
969 ///
970 /// Note that the lint is distinct from the `unused_macros` lint, which
971 /// fires if the entire macro is never called, while this lint fires for
972 /// single unused rules of the macro that is otherwise used.
973 /// `unused_macro_rules` fires only if `unused_macros` wouldn't fire.
974 ///
975 /// ### Example
976 ///
977 /// ```rust
978 /// #[warn(unused_macro_rules)]
979 /// macro_rules! unused_empty {
980 /// (hello) => { println!("Hello, world!") }; // This rule is unused
981 /// () => { println!("empty") }; // This rule is used
982 /// }
983 ///
984 /// fn main() {
985 /// unused_empty!(hello);
986 /// }
987 /// ```
988 ///
989 /// {{produces}}
990 ///
991 /// ### Explanation
992 ///
993 /// Unused macro rules may signal a mistake or unfinished code. Furthermore,
994 /// they slow down compilation. Right now, silencing the warning is not
995 /// supported on a single rule level, so you have to add an allow to the
996 /// entire macro definition.
997 ///
998 /// If you intended to export the macro to make it
999 /// available outside of the crate, use the [`macro_export` attribute].
1000 ///
1001 /// [`macro_export` attribute]: https://doc.rust-lang.org/reference/macros-by-example.html#path-based-scope
1002 pub UNUSED_MACRO_RULES,
1003 Allow,
1004 "detects macro rules that were not used"
1005}
1006
1007declare_lint! {
1008 /// The `warnings` lint allows you to change the level of other
1009 /// lints which produce warnings.
1010 ///
1011 /// ### Example
1012 ///
1013 /// ```rust
1014 /// #![deny(warnings)]
1015 /// fn foo() {}
1016 /// ```
1017 ///
1018 /// {{produces}}
1019 ///
1020 /// ### Explanation
1021 ///
1022 /// The `warnings` lint is a bit special; by changing its level, you
1023 /// change every other warning that would produce a warning to whatever
1024 /// value you'd like. As such, you won't ever trigger this lint in your
1025 /// code directly.
1026 pub WARNINGS,
1027 Warn,
1028 "mass-change the level for lints which produce warnings"
1029}
1030
1031declare_lint! {
1032 /// The `unused_features` lint detects unused or unknown features found in
1033 /// crate-level [`feature` attributes].
1034 ///
1035 /// [`feature` attributes]: https://doc.rust-lang.org/nightly/unstable-book/
1036 ///
1037 /// Note: This lint is currently not functional, see [issue #44232] for
1038 /// more details.
1039 ///
1040 /// [issue #44232]: https://github.com/rust-lang/rust/issues/44232
1041 pub UNUSED_FEATURES,
1042 Warn,
1043 "unused features found in crate-level `#[feature]` directives"
1044}
1045
1046declare_lint! {
1047 /// The `stable_features` lint detects a [`feature` attribute] that
1048 /// has since been made stable.
1049 ///
1050 /// [`feature` attribute]: https://doc.rust-lang.org/nightly/unstable-book/
1051 ///
1052 /// ### Example
1053 ///
1054 /// ```rust
1055 /// #![feature(test_accepted_feature)]
1056 /// fn main() {}
1057 /// ```
1058 ///
1059 /// {{produces}}
1060 ///
1061 /// ### Explanation
1062 ///
1063 /// When a feature is stabilized, it is no longer necessary to include a
1064 /// `#![feature]` attribute for it. To fix, simply remove the
1065 /// `#![feature]` attribute.
1066 pub STABLE_FEATURES,
1067 Warn,
1068 "stable features found in `#[feature]` directive"
1069}
1070
1071declare_lint! {
1072 /// The `unknown_crate_types` lint detects an unknown crate type found in
1073 /// a [`crate_type` attribute].
1074 ///
1075 /// ### Example
1076 ///
1077 /// ```rust,compile_fail
1078 /// #![crate_type="lol"]
1079 /// fn main() {}
1080 /// ```
1081 ///
1082 /// {{produces}}
1083 ///
1084 /// ### Explanation
1085 ///
1086 /// An unknown value give to the `crate_type` attribute is almost
1087 /// certainly a mistake.
1088 ///
1089 /// [`crate_type` attribute]: https://doc.rust-lang.org/reference/linkage.html
1090 pub UNKNOWN_CRATE_TYPES,
1091 Deny,
1092 "unknown crate type found in `#[crate_type]` directive",
1093 crate_level_only
1094}
1095
1096declare_lint! {
1097 /// The `trivial_casts` lint detects trivial casts which could be replaced
1098 /// with coercion, which may require a temporary variable.
1099 ///
1100 /// ### Example
1101 ///
1102 /// ```rust,compile_fail
1103 /// #![deny(trivial_casts)]
1104 /// let x: &u32 = &42;
1105 /// let y = x as *const u32;
1106 /// ```
1107 ///
1108 /// {{produces}}
1109 ///
1110 /// ### Explanation
1111 ///
1112 /// A trivial cast is a cast `e as T` where `e` has type `U` and `U` is a
1113 /// subtype of `T`. This type of cast is usually unnecessary, as it can be
1114 /// usually be inferred.
1115 ///
1116 /// This lint is "allow" by default because there are situations, such as
1117 /// with FFI interfaces or complex type aliases, where it triggers
1118 /// incorrectly, or in situations where it will be more difficult to
1119 /// clearly express the intent. It may be possible that this will become a
1120 /// warning in the future, possibly with an explicit syntax for coercions
1121 /// providing a convenient way to work around the current issues.
1122 /// See [RFC 401 (coercions)][rfc-401], [RFC 803 (type ascription)][rfc-803] and
1123 /// [RFC 3307 (remove type ascription)][rfc-3307] for historical context.
1124 ///
1125 /// [rfc-401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
1126 /// [rfc-803]: https://github.com/rust-lang/rfcs/blob/master/text/0803-type-ascription.md
1127 /// [rfc-3307]: https://github.com/rust-lang/rfcs/blob/master/text/3307-de-rfc-type-ascription.md
1128 pub TRIVIAL_CASTS,
1129 Allow,
1130 "detects trivial casts which could be removed"
1131}
1132
1133declare_lint! {
1134 /// The `trivial_numeric_casts` lint detects trivial numeric casts of types
1135 /// which could be removed.
1136 ///
1137 /// ### Example
1138 ///
1139 /// ```rust,compile_fail
1140 /// #![deny(trivial_numeric_casts)]
1141 /// let x = 42_i32 as i32;
1142 /// ```
1143 ///
1144 /// {{produces}}
1145 ///
1146 /// ### Explanation
1147 ///
1148 /// A trivial numeric cast is a cast of a numeric type to the same numeric
1149 /// type. This type of cast is usually unnecessary.
1150 ///
1151 /// This lint is "allow" by default because there are situations, such as
1152 /// with FFI interfaces or complex type aliases, where it triggers
1153 /// incorrectly, or in situations where it will be more difficult to
1154 /// clearly express the intent. It may be possible that this will become a
1155 /// warning in the future, possibly with an explicit syntax for coercions
1156 /// providing a convenient way to work around the current issues.
1157 /// See [RFC 401 (coercions)][rfc-401], [RFC 803 (type ascription)][rfc-803] and
1158 /// [RFC 3307 (remove type ascription)][rfc-3307] for historical context.
1159 ///
1160 /// [rfc-401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
1161 /// [rfc-803]: https://github.com/rust-lang/rfcs/blob/master/text/0803-type-ascription.md
1162 /// [rfc-3307]: https://github.com/rust-lang/rfcs/blob/master/text/3307-de-rfc-type-ascription.md
1163 pub TRIVIAL_NUMERIC_CASTS,
1164 Allow,
1165 "detects trivial casts of numeric types which could be removed"
1166}
1167
1168declare_lint! {
1169 /// The `exported_private_dependencies` lint detects private dependencies
1170 /// that are exposed in a public interface.
1171 ///
1172 /// ### Example
1173 ///
1174 /// ```rust,ignore (needs-dependency)
1175 /// pub fn foo() -> Option<some_private_dependency::Thing> {
1176 /// None
1177 /// }
1178 /// ```
1179 ///
1180 /// This will produce:
1181 ///
1182 /// ```text
1183 /// warning: type `bar::Thing` from private dependency 'bar' in public interface
1184 /// --> src/lib.rs:3:1
1185 /// |
1186 /// 3 | pub fn foo() -> Option<bar::Thing> {
1187 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1188 /// |
1189 /// = note: `#[warn(exported_private_dependencies)]` on by default
1190 /// ```
1191 ///
1192 /// ### Explanation
1193 ///
1194 /// Dependencies can be marked as "private" to indicate that they are not
1195 /// exposed in the public interface of a crate. This can be used by Cargo
1196 /// to independently resolve those dependencies because it can assume it
1197 /// does not need to unify them with other packages using that same
1198 /// dependency. This lint is an indication of a violation of that
1199 /// contract.
1200 ///
1201 /// To fix this, avoid exposing the dependency in your public interface.
1202 /// Or, switch the dependency to a public dependency.
1203 ///
1204 /// Note that support for this is only available on the nightly channel.
1205 /// See [RFC 1977] for more details, as well as the [Cargo documentation].
1206 ///
1207 /// [RFC 1977]: https://github.com/rust-lang/rfcs/blob/master/text/1977-public-private-dependencies.md
1208 /// [Cargo documentation]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#public-dependency
1209 pub EXPORTED_PRIVATE_DEPENDENCIES,
1210 Warn,
1211 "public interface leaks type from a private dependency"
1212}
1213
1214declare_lint! {
1215 /// The `pub_use_of_private_extern_crate` lint detects a specific
1216 /// situation of re-exporting a private `extern crate`.
1217 ///
1218 /// ### Example
1219 ///
1220 /// ```rust,compile_fail
1221 /// extern crate core;
1222 /// pub use core as reexported_core;
1223 /// ```
1224 ///
1225 /// {{produces}}
1226 ///
1227 /// ### Explanation
1228 ///
1229 /// A public `use` declaration should not be used to publicly re-export a
1230 /// private `extern crate`. `pub extern crate` should be used instead.
1231 ///
1232 /// This was historically allowed, but is not the intended behavior
1233 /// according to the visibility rules. This is a [future-incompatible]
1234 /// lint to transition this to a hard error in the future. See [issue
1235 /// #127909] for more details.
1236 ///
1237 /// [issue #127909]: https://github.com/rust-lang/rust/issues/127909
1238 /// [future-incompatible]: ../index.md#future-incompatible-lints
1239 pub PUB_USE_OF_PRIVATE_EXTERN_CRATE,
1240 Deny,
1241 "detect public re-exports of private extern crates",
1242 @future_incompatible = FutureIncompatibleInfo {
1243 reason: FutureIncompatibilityReason::FutureReleaseError,
1244 reference: "issue #127909 <https://github.com/rust-lang/rust/issues/127909>",
1245 report_in_deps: true,
1246 };
1247}
1248
1249declare_lint! {
1250 /// The `invalid_type_param_default` lint detects type parameter defaults
1251 /// erroneously allowed in an invalid location.
1252 ///
1253 /// ### Example
1254 ///
1255 /// ```rust,compile_fail
1256 /// fn foo<T=i32>(t: T) {}
1257 /// ```
1258 ///
1259 /// {{produces}}
1260 ///
1261 /// ### Explanation
1262 ///
1263 /// Default type parameters were only intended to be allowed in certain
1264 /// situations, but historically the compiler allowed them everywhere.
1265 /// This is a [future-incompatible] lint to transition this to a hard
1266 /// error in the future. See [issue #36887] for more details.
1267 ///
1268 /// [issue #36887]: https://github.com/rust-lang/rust/issues/36887
1269 /// [future-incompatible]: ../index.md#future-incompatible-lints
1270 pub INVALID_TYPE_PARAM_DEFAULT,
1271 Deny,
1272 "type parameter default erroneously allowed in invalid location",
1273 @future_incompatible = FutureIncompatibleInfo {
1274 reason: FutureIncompatibilityReason::FutureReleaseError,
1275 reference: "issue #36887 <https://github.com/rust-lang/rust/issues/36887>",
1276 report_in_deps: true,
1277 };
1278}
1279
1280declare_lint! {
1281 /// The `renamed_and_removed_lints` lint detects lints that have been
1282 /// renamed or removed.
1283 ///
1284 /// ### Example
1285 ///
1286 /// ```rust
1287 /// #![deny(raw_pointer_derive)]
1288 /// ```
1289 ///
1290 /// {{produces}}
1291 ///
1292 /// ### Explanation
1293 ///
1294 /// To fix this, either remove the lint or use the new name. This can help
1295 /// avoid confusion about lints that are no longer valid, and help
1296 /// maintain consistency for renamed lints.
1297 pub RENAMED_AND_REMOVED_LINTS,
1298 Warn,
1299 "lints that have been renamed or removed"
1300}
1301
1302declare_lint! {
1303 /// The `const_item_mutation` lint detects attempts to mutate a `const`
1304 /// item.
1305 ///
1306 /// ### Example
1307 ///
1308 /// ```rust
1309 /// const FOO: [i32; 1] = [0];
1310 ///
1311 /// fn main() {
1312 /// FOO[0] = 1;
1313 /// // This will print "[0]".
1314 /// println!("{:?}", FOO);
1315 /// }
1316 /// ```
1317 ///
1318 /// {{produces}}
1319 ///
1320 /// ### Explanation
1321 ///
1322 /// Trying to directly mutate a `const` item is almost always a mistake.
1323 /// What is happening in the example above is that a temporary copy of the
1324 /// `const` is mutated, but the original `const` is not. Each time you
1325 /// refer to the `const` by name (such as `FOO` in the example above), a
1326 /// separate copy of the value is inlined at that location.
1327 ///
1328 /// This lint checks for writing directly to a field (`FOO.field =
1329 /// some_value`) or array entry (`FOO[0] = val`), or taking a mutable
1330 /// reference to the const item (`&mut FOO`), including through an
1331 /// autoderef (`FOO.some_mut_self_method()`).
1332 ///
1333 /// There are various alternatives depending on what you are trying to
1334 /// accomplish:
1335 ///
1336 /// * First, always reconsider using mutable globals, as they can be
1337 /// difficult to use correctly, and can make the code more difficult to
1338 /// use or understand.
1339 /// * If you are trying to perform a one-time initialization of a global:
1340 /// * If the value can be computed at compile-time, consider using
1341 /// const-compatible values (see [Constant Evaluation]).
1342 /// * For more complex single-initialization cases, consider using
1343 /// [`std::sync::LazyLock`].
1344 /// * If you truly need a mutable global, consider using a [`static`],
1345 /// which has a variety of options:
1346 /// * Simple data types can be directly defined and mutated with an
1347 /// [`atomic`] type.
1348 /// * More complex types can be placed in a synchronization primitive
1349 /// like a [`Mutex`], which can be initialized with one of the options
1350 /// listed above.
1351 /// * A [mutable `static`] is a low-level primitive, requiring unsafe.
1352 /// Typically This should be avoided in preference of something
1353 /// higher-level like one of the above.
1354 ///
1355 /// [Constant Evaluation]: https://doc.rust-lang.org/reference/const_eval.html
1356 /// [`static`]: https://doc.rust-lang.org/reference/items/static-items.html
1357 /// [mutable `static`]: https://doc.rust-lang.org/reference/items/static-items.html#mutable-statics
1358 /// [`std::sync::LazyLock`]: https://doc.rust-lang.org/stable/std/sync/struct.LazyLock.html
1359 /// [`atomic`]: https://doc.rust-lang.org/std/sync/atomic/index.html
1360 /// [`Mutex`]: https://doc.rust-lang.org/std/sync/struct.Mutex.html
1361 pub CONST_ITEM_MUTATION,
1362 Warn,
1363 "detects attempts to mutate a `const` item",
1364}
1365
1366declare_lint! {
1367 /// The `patterns_in_fns_without_body` lint detects `mut` identifier
1368 /// patterns as a parameter in functions without a body.
1369 ///
1370 /// ### Example
1371 ///
1372 /// ```rust,compile_fail
1373 /// trait Trait {
1374 /// fn foo(mut arg: u8);
1375 /// }
1376 /// ```
1377 ///
1378 /// {{produces}}
1379 ///
1380 /// ### Explanation
1381 ///
1382 /// To fix this, remove `mut` from the parameter in the trait definition;
1383 /// it can be used in the implementation. That is, the following is OK:
1384 ///
1385 /// ```rust
1386 /// trait Trait {
1387 /// fn foo(arg: u8); // Removed `mut` here
1388 /// }
1389 ///
1390 /// impl Trait for i32 {
1391 /// fn foo(mut arg: u8) { // `mut` here is OK
1392 ///
1393 /// }
1394 /// }
1395 /// ```
1396 ///
1397 /// Trait definitions can define functions without a body to specify a
1398 /// function that implementors must define. The parameter names in the
1399 /// body-less functions are only allowed to be `_` or an [identifier] for
1400 /// documentation purposes (only the type is relevant). Previous versions
1401 /// of the compiler erroneously allowed [identifier patterns] with the
1402 /// `mut` keyword, but this was not intended to be allowed. This is a
1403 /// [future-incompatible] lint to transition this to a hard error in the
1404 /// future. See [issue #35203] for more details.
1405 ///
1406 /// [identifier]: https://doc.rust-lang.org/reference/identifiers.html
1407 /// [identifier patterns]: https://doc.rust-lang.org/reference/patterns.html#identifier-patterns
1408 /// [issue #35203]: https://github.com/rust-lang/rust/issues/35203
1409 /// [future-incompatible]: ../index.md#future-incompatible-lints
1410 pub PATTERNS_IN_FNS_WITHOUT_BODY,
1411 Deny,
1412 "patterns in functions without body were erroneously allowed",
1413 @future_incompatible = FutureIncompatibleInfo {
1414 reason: FutureIncompatibilityReason::FutureReleaseError,
1415 reference: "issue #35203 <https://github.com/rust-lang/rust/issues/35203>",
1416 };
1417}
1418
1419declare_lint! {
1420 /// The `late_bound_lifetime_arguments` lint detects generic lifetime
1421 /// arguments in path segments with late bound lifetime parameters.
1422 ///
1423 /// ### Example
1424 ///
1425 /// ```rust
1426 /// struct S;
1427 ///
1428 /// impl S {
1429 /// fn late(self, _: &u8, _: &u8) {}
1430 /// }
1431 ///
1432 /// fn main() {
1433 /// S.late::<'static>(&0, &0);
1434 /// }
1435 /// ```
1436 ///
1437 /// {{produces}}
1438 ///
1439 /// ### Explanation
1440 ///
1441 /// It is not clear how to provide arguments for early-bound lifetime
1442 /// parameters if they are intermixed with late-bound parameters in the
1443 /// same list. For now, providing any explicit arguments will trigger this
1444 /// lint if late-bound parameters are present, so in the future a solution
1445 /// can be adopted without hitting backward compatibility issues. This is
1446 /// a [future-incompatible] lint to transition this to a hard error in the
1447 /// future. See [issue #42868] for more details, along with a description
1448 /// of the difference between early and late-bound parameters.
1449 ///
1450 /// [issue #42868]: https://github.com/rust-lang/rust/issues/42868
1451 /// [future-incompatible]: ../index.md#future-incompatible-lints
1452 pub LATE_BOUND_LIFETIME_ARGUMENTS,
1453 Warn,
1454 "detects generic lifetime arguments in path segments with late bound lifetime parameters",
1455 @future_incompatible = FutureIncompatibleInfo {
1456 reason: FutureIncompatibilityReason::FutureReleaseError,
1457 reference: "issue #42868 <https://github.com/rust-lang/rust/issues/42868>",
1458 };
1459}
1460
1461declare_lint! {
1462 /// The `coherence_leak_check` lint detects conflicting implementations of
1463 /// a trait that are only distinguished by the old leak-check code.
1464 ///
1465 /// ### Example
1466 ///
1467 /// ```rust
1468 /// trait SomeTrait { }
1469 /// impl SomeTrait for for<'a> fn(&'a u8) { }
1470 /// impl<'a> SomeTrait for fn(&'a u8) { }
1471 /// ```
1472 ///
1473 /// {{produces}}
1474 ///
1475 /// ### Explanation
1476 ///
1477 /// In the past, the compiler would accept trait implementations for
1478 /// identical functions that differed only in where the lifetime binder
1479 /// appeared. Due to a change in the borrow checker implementation to fix
1480 /// several bugs, this is no longer allowed. However, since this affects
1481 /// existing code, this is a [future-incompatible] lint to transition this
1482 /// to a hard error in the future.
1483 ///
1484 /// Code relying on this pattern should introduce "[newtypes]",
1485 /// like `struct Foo(for<'a> fn(&'a u8))`.
1486 ///
1487 /// See [issue #56105] for more details.
1488 ///
1489 /// [issue #56105]: https://github.com/rust-lang/rust/issues/56105
1490 /// [newtypes]: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#using-the-newtype-pattern-for-type-safety-and-abstraction
1491 /// [future-incompatible]: ../index.md#future-incompatible-lints
1492 pub COHERENCE_LEAK_CHECK,
1493 Warn,
1494 "distinct impls distinguished only by the leak-check code",
1495 @future_incompatible = FutureIncompatibleInfo {
1496 reason: FutureIncompatibilityReason::Custom("the behavior may change in a future release"),
1497 reference: "issue #56105 <https://github.com/rust-lang/rust/issues/56105>",
1498 };
1499}
1500
1501declare_lint! {
1502 /// The `deprecated` lint detects use of deprecated items.
1503 ///
1504 /// ### Example
1505 ///
1506 /// ```rust
1507 /// #[deprecated]
1508 /// fn foo() {}
1509 ///
1510 /// fn bar() {
1511 /// foo();
1512 /// }
1513 /// ```
1514 ///
1515 /// {{produces}}
1516 ///
1517 /// ### Explanation
1518 ///
1519 /// Items may be marked "deprecated" with the [`deprecated` attribute] to
1520 /// indicate that they should no longer be used. Usually the attribute
1521 /// should include a note on what to use instead, or check the
1522 /// documentation.
1523 ///
1524 /// [`deprecated` attribute]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute
1525 pub DEPRECATED,
1526 Warn,
1527 "detects use of deprecated items",
1528 report_in_external_macro
1529}
1530
1531declare_lint! {
1532 /// The `unused_unsafe` lint detects unnecessary use of an `unsafe` block.
1533 ///
1534 /// ### Example
1535 ///
1536 /// ```rust
1537 /// unsafe {}
1538 /// ```
1539 ///
1540 /// {{produces}}
1541 ///
1542 /// ### Explanation
1543 ///
1544 /// If nothing within the block requires `unsafe`, then remove the
1545 /// `unsafe` marker because it is not required and may cause confusion.
1546 pub UNUSED_UNSAFE,
1547 Warn,
1548 "unnecessary use of an `unsafe` block"
1549}
1550
1551declare_lint! {
1552 /// The `unused_mut` lint detects mut variables which don't need to be
1553 /// mutable.
1554 ///
1555 /// ### Example
1556 ///
1557 /// ```rust
1558 /// let mut x = 5;
1559 /// ```
1560 ///
1561 /// {{produces}}
1562 ///
1563 /// ### Explanation
1564 ///
1565 /// The preferred style is to only mark variables as `mut` if it is
1566 /// required.
1567 pub UNUSED_MUT,
1568 Warn,
1569 "detect mut variables which don't need to be mutable"
1570}
1571
1572declare_lint! {
1573 /// The `rust_2024_incompatible_pat` lint
1574 /// detects patterns whose meaning will change in the Rust 2024 edition.
1575 ///
1576 /// ### Example
1577 ///
1578 /// ```rust,edition2021
1579 /// #![warn(rust_2024_incompatible_pat)]
1580 ///
1581 /// if let Some(&a) = &Some(&0u8) {
1582 /// let _: u8 = a;
1583 /// }
1584 /// if let Some(mut _a) = &mut Some(0u8) {
1585 /// _a = 7u8;
1586 /// }
1587 /// ```
1588 ///
1589 /// {{produces}}
1590 ///
1591 /// ### Explanation
1592 ///
1593 /// In Rust 2024 and above, the `mut` keyword does not reset the pattern binding mode,
1594 /// and nor do `&` or `&mut` patterns. The lint will suggest code that
1595 /// has the same meaning in all editions.
1596 pub RUST_2024_INCOMPATIBLE_PAT,
1597 Allow,
1598 "detects patterns whose meaning will change in Rust 2024",
1599 @future_incompatible = FutureIncompatibleInfo {
1600 reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024),
1601 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>",
1602 };
1603}
1604
1605declare_lint! {
1606 /// The `unconditional_recursion` lint detects functions that cannot
1607 /// return without calling themselves.
1608 ///
1609 /// ### Example
1610 ///
1611 /// ```rust
1612 /// fn foo() {
1613 /// foo();
1614 /// }
1615 /// ```
1616 ///
1617 /// {{produces}}
1618 ///
1619 /// ### Explanation
1620 ///
1621 /// It is usually a mistake to have a recursive call that does not have
1622 /// some condition to cause it to terminate. If you really intend to have
1623 /// an infinite loop, using a `loop` expression is recommended.
1624 pub UNCONDITIONAL_RECURSION,
1625 Warn,
1626 "functions that cannot return without calling themselves"
1627}
1628
1629declare_lint! {
1630 /// The `single_use_lifetimes` lint detects lifetimes that are only used
1631 /// once.
1632 ///
1633 /// ### Example
1634 ///
1635 /// ```rust,compile_fail
1636 /// #![deny(single_use_lifetimes)]
1637 ///
1638 /// fn foo<'a>(x: &'a u32) {}
1639 /// ```
1640 ///
1641 /// {{produces}}
1642 ///
1643 /// ### Explanation
1644 ///
1645 /// Specifying an explicit lifetime like `'a` in a function or `impl`
1646 /// should only be used to link together two things. Otherwise, you should
1647 /// just use `'_` to indicate that the lifetime is not linked to anything,
1648 /// or elide the lifetime altogether if possible.
1649 ///
1650 /// This lint is "allow" by default because it was introduced at a time
1651 /// when `'_` and elided lifetimes were first being introduced, and this
1652 /// lint would be too noisy. Also, there are some known false positives
1653 /// that it produces. See [RFC 2115] for historical context, and [issue
1654 /// #44752] for more details.
1655 ///
1656 /// [RFC 2115]: https://github.com/rust-lang/rfcs/blob/master/text/2115-argument-lifetimes.md
1657 /// [issue #44752]: https://github.com/rust-lang/rust/issues/44752
1658 pub SINGLE_USE_LIFETIMES,
1659 Allow,
1660 "detects lifetime parameters that are only used once"
1661}
1662
1663declare_lint! {
1664 /// The `unused_lifetimes` lint detects lifetime parameters that are never
1665 /// used.
1666 ///
1667 /// ### Example
1668 ///
1669 /// ```rust,compile_fail
1670 /// #[deny(unused_lifetimes)]
1671 ///
1672 /// pub fn foo<'a>() {}
1673 /// ```
1674 ///
1675 /// {{produces}}
1676 ///
1677 /// ### Explanation
1678 ///
1679 /// Unused lifetime parameters may signal a mistake or unfinished code.
1680 /// Consider removing the parameter.
1681 pub UNUSED_LIFETIMES,
1682 Allow,
1683 "detects lifetime parameters that are never used"
1684}
1685
1686declare_lint! {
1687 /// The `redundant_lifetimes` lint detects lifetime parameters that are
1688 /// redundant because they are equal to another named lifetime.
1689 ///
1690 /// ### Example
1691 ///
1692 /// ```rust,compile_fail
1693 /// #[deny(redundant_lifetimes)]
1694 ///
1695 /// // `'a = 'static`, so all usages of `'a` can be replaced with `'static`
1696 /// pub fn bar<'a: 'static>() {}
1697 ///
1698 /// // `'a = 'b`, so all usages of `'b` can be replaced with `'a`
1699 /// pub fn bar<'a: 'b, 'b: 'a>() {}
1700 /// ```
1701 ///
1702 /// {{produces}}
1703 ///
1704 /// ### Explanation
1705 ///
1706 /// Unused lifetime parameters may signal a mistake or unfinished code.
1707 /// Consider removing the parameter.
1708 pub REDUNDANT_LIFETIMES,
1709 Allow,
1710 "detects lifetime parameters that are redundant because they are equal to some other named lifetime"
1711}
1712
1713declare_lint! {
1714 /// The `tyvar_behind_raw_pointer` lint detects raw pointer to an
1715 /// inference variable.
1716 ///
1717 /// ### Example
1718 ///
1719 /// ```rust,edition2015
1720 /// // edition 2015
1721 /// let data = std::ptr::null();
1722 /// let _ = &data as *const *const ();
1723 ///
1724 /// if data.is_null() {}
1725 /// ```
1726 ///
1727 /// {{produces}}
1728 ///
1729 /// ### Explanation
1730 ///
1731 /// This kind of inference was previously allowed, but with the future
1732 /// arrival of [arbitrary self types], this can introduce ambiguity. To
1733 /// resolve this, use an explicit type instead of relying on type
1734 /// inference.
1735 ///
1736 /// This is a [future-incompatible] lint to transition this to a hard
1737 /// error in the 2018 edition. See [issue #46906] for more details. This
1738 /// is currently a hard-error on the 2018 edition, and is "warn" by
1739 /// default in the 2015 edition.
1740 ///
1741 /// [arbitrary self types]: https://github.com/rust-lang/rust/issues/44874
1742 /// [issue #46906]: https://github.com/rust-lang/rust/issues/46906
1743 /// [future-incompatible]: ../index.md#future-incompatible-lints
1744 pub TYVAR_BEHIND_RAW_POINTER,
1745 Warn,
1746 "raw pointer to an inference variable",
1747 @future_incompatible = FutureIncompatibleInfo {
1748 reason: FutureIncompatibilityReason::EditionError(Edition::Edition2018),
1749 reference: "issue #46906 <https://github.com/rust-lang/rust/issues/46906>",
1750 };
1751}
1752
1753declare_lint! {
1754 /// The `elided_lifetimes_in_paths` lint detects the use of hidden
1755 /// lifetime parameters.
1756 ///
1757 /// ### Example
1758 ///
1759 /// ```rust,compile_fail
1760 /// #![deny(elided_lifetimes_in_paths)]
1761 /// #![deny(warnings)]
1762 /// struct Foo<'a> {
1763 /// x: &'a u32
1764 /// }
1765 ///
1766 /// fn foo(x: &Foo) {
1767 /// }
1768 /// ```
1769 ///
1770 /// {{produces}}
1771 ///
1772 /// ### Explanation
1773 ///
1774 /// Elided lifetime parameters can make it difficult to see at a glance
1775 /// that borrowing is occurring. This lint ensures that lifetime
1776 /// parameters are always explicitly stated, even if it is the `'_`
1777 /// [placeholder lifetime].
1778 ///
1779 /// This lint is "allow" by default because it has some known issues, and
1780 /// may require a significant transition for old code.
1781 ///
1782 /// [placeholder lifetime]: https://doc.rust-lang.org/reference/lifetime-elision.html#lifetime-elision-in-functions
1783 pub ELIDED_LIFETIMES_IN_PATHS,
1784 Allow,
1785 "hidden lifetime parameters in types are deprecated"
1786}
1787
1788declare_lint! {
1789 /// The `bare_trait_objects` lint suggests using `dyn Trait` for trait
1790 /// objects.
1791 ///
1792 /// ### Example
1793 ///
1794 /// ```rust,edition2018
1795 /// trait Trait { }
1796 ///
1797 /// fn takes_trait_object(_: Box<Trait>) {
1798 /// }
1799 /// ```
1800 ///
1801 /// {{produces}}
1802 ///
1803 /// ### Explanation
1804 ///
1805 /// Without the `dyn` indicator, it can be ambiguous or confusing when
1806 /// reading code as to whether or not you are looking at a trait object.
1807 /// The `dyn` keyword makes it explicit, and adds a symmetry to contrast
1808 /// with [`impl Trait`].
1809 ///
1810 /// [`impl Trait`]: https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
1811 pub BARE_TRAIT_OBJECTS,
1812 Warn,
1813 "suggest using `dyn Trait` for trait objects",
1814 @future_incompatible = FutureIncompatibleInfo {
1815 reason: FutureIncompatibilityReason::EditionError(Edition::Edition2021),
1816 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>",
1817 };
1818}
1819
1820declare_lint! {
1821 /// The `absolute_paths_not_starting_with_crate` lint detects fully
1822 /// qualified paths that start with a module name instead of `crate`,
1823 /// `self`, or an extern crate name
1824 ///
1825 /// ### Example
1826 ///
1827 /// ```rust,edition2015,compile_fail
1828 /// #![deny(absolute_paths_not_starting_with_crate)]
1829 ///
1830 /// mod foo {
1831 /// pub fn bar() {}
1832 /// }
1833 ///
1834 /// fn main() {
1835 /// ::foo::bar();
1836 /// }
1837 /// ```
1838 ///
1839 /// {{produces}}
1840 ///
1841 /// ### Explanation
1842 ///
1843 /// Rust [editions] allow the language to evolve without breaking
1844 /// backwards compatibility. This lint catches code that uses absolute
1845 /// paths in the style of the 2015 edition. In the 2015 edition, absolute
1846 /// paths (those starting with `::`) refer to either the crate root or an
1847 /// external crate. In the 2018 edition it was changed so that they only
1848 /// refer to external crates. The path prefix `crate::` should be used
1849 /// instead to reference items from the crate root.
1850 ///
1851 /// If you switch the compiler from the 2015 to 2018 edition without
1852 /// updating the code, then it will fail to compile if the old style paths
1853 /// are used. You can manually change the paths to use the `crate::`
1854 /// prefix to transition to the 2018 edition.
1855 ///
1856 /// This lint solves the problem automatically. It is "allow" by default
1857 /// because the code is perfectly valid in the 2015 edition. The [`cargo
1858 /// fix`] tool with the `--edition` flag will switch this lint to "warn"
1859 /// and automatically apply the suggested fix from the compiler. This
1860 /// provides a completely automated way to update old code to the 2018
1861 /// edition.
1862 ///
1863 /// [editions]: https://doc.rust-lang.org/edition-guide/
1864 /// [`cargo fix`]: https://doc.rust-lang.org/cargo/commands/cargo-fix.html
1865 pub ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
1866 Allow,
1867 "fully qualified paths that start with a module name \
1868 instead of `crate`, `self`, or an extern crate name",
1869 @future_incompatible = FutureIncompatibleInfo {
1870 reason: FutureIncompatibilityReason::EditionError(Edition::Edition2018),
1871 reference: "issue #53130 <https://github.com/rust-lang/rust/issues/53130>",
1872 };
1873}
1874
1875declare_lint! {
1876 /// The `unstable_name_collisions` lint detects that you have used a name
1877 /// that the standard library plans to add in the future.
1878 ///
1879 /// ### Example
1880 ///
1881 /// ```rust
1882 /// trait MyIterator : Iterator {
1883 /// // is_partitioned is an unstable method that already exists on the Iterator trait
1884 /// fn is_partitioned<P>(self, predicate: P) -> bool
1885 /// where
1886 /// Self: Sized,
1887 /// P: FnMut(Self::Item) -> bool,
1888 /// {true}
1889 /// }
1890 ///
1891 /// impl<T: ?Sized> MyIterator for T where T: Iterator { }
1892 ///
1893 /// let x = vec![1, 2, 3];
1894 /// let _ = x.iter().is_partitioned(|_| true);
1895 /// ```
1896 ///
1897 /// {{produces}}
1898 ///
1899 /// ### Explanation
1900 ///
1901 /// When new methods are added to traits in the standard library, they are
1902 /// usually added in an "unstable" form which is only available on the
1903 /// [nightly channel] with a [`feature` attribute]. If there is any
1904 /// preexisting code which extends a trait to have a method with the same
1905 /// name, then the names will collide. In the future, when the method is
1906 /// stabilized, this will cause an error due to the ambiguity. This lint
1907 /// is an early-warning to let you know that there may be a collision in
1908 /// the future. This can be avoided by adding type annotations to
1909 /// disambiguate which trait method you intend to call, such as
1910 /// `MyIterator::is_partitioned(my_iter, my_predicate)` or renaming or removing the method.
1911 ///
1912 /// [nightly channel]: https://doc.rust-lang.org/book/appendix-07-nightly-rust.html
1913 /// [`feature` attribute]: https://doc.rust-lang.org/nightly/unstable-book/
1914 pub UNSTABLE_NAME_COLLISIONS,
1915 Warn,
1916 "detects name collision with an existing but unstable method",
1917 @future_incompatible = FutureIncompatibleInfo {
1918 reason: FutureIncompatibilityReason::Custom(
1919 "once this associated item is added to the standard library, \
1920 the ambiguity may cause an error or change in behavior!"
1921 ),
1922 reference: "issue #48919 <https://github.com/rust-lang/rust/issues/48919>",
1923 // Note: this item represents future incompatibility of all unstable functions in the
1924 // standard library, and thus should never be removed or changed to an error.
1925 };
1926}
1927
1928declare_lint! {
1929 /// The `irrefutable_let_patterns` lint detects [irrefutable patterns]
1930 /// in [`if let`]s, [`while let`]s, and `if let` guards.
1931 ///
1932 /// ### Example
1933 ///
1934 /// ```rust
1935 /// if let _ = 123 {
1936 /// println!("always runs!");
1937 /// }
1938 /// ```
1939 ///
1940 /// {{produces}}
1941 ///
1942 /// ### Explanation
1943 ///
1944 /// There usually isn't a reason to have an irrefutable pattern in an
1945 /// `if let` or `while let` statement, because the pattern will always match
1946 /// successfully. A [`let`] or [`loop`] statement will suffice. However,
1947 /// when generating code with a macro, forbidding irrefutable patterns
1948 /// would require awkward workarounds in situations where the macro
1949 /// doesn't know if the pattern is refutable or not. This lint allows
1950 /// macros to accept this form, while alerting for a possibly incorrect
1951 /// use in normal code.
1952 ///
1953 /// See [RFC 2086] for more details.
1954 ///
1955 /// [irrefutable patterns]: https://doc.rust-lang.org/reference/patterns.html#refutability
1956 /// [`if let`]: https://doc.rust-lang.org/reference/expressions/if-expr.html#if-let-expressions
1957 /// [`while let`]: https://doc.rust-lang.org/reference/expressions/loop-expr.html#predicate-pattern-loops
1958 /// [`let`]: https://doc.rust-lang.org/reference/statements.html#let-statements
1959 /// [`loop`]: https://doc.rust-lang.org/reference/expressions/loop-expr.html#infinite-loops
1960 /// [RFC 2086]: https://github.com/rust-lang/rfcs/blob/master/text/2086-allow-if-let-irrefutables.md
1961 pub IRREFUTABLE_LET_PATTERNS,
1962 Warn,
1963 "detects irrefutable patterns in `if let` and `while let` statements"
1964}
1965
1966declare_lint! {
1967 /// The `unused_labels` lint detects [labels] that are never used.
1968 ///
1969 /// [labels]: https://doc.rust-lang.org/reference/expressions/loop-expr.html#loop-labels
1970 ///
1971 /// ### Example
1972 ///
1973 /// ```rust,no_run
1974 /// 'unused_label: loop {}
1975 /// ```
1976 ///
1977 /// {{produces}}
1978 ///
1979 /// ### Explanation
1980 ///
1981 /// Unused labels may signal a mistake or unfinished code. To silence the
1982 /// warning for the individual label, prefix it with an underscore such as
1983 /// `'_my_label:`.
1984 pub UNUSED_LABELS,
1985 Warn,
1986 "detects labels that are never used"
1987}
1988
1989declare_lint! {
1990 /// The `proc_macro_derive_resolution_fallback` lint detects proc macro
1991 /// derives using inaccessible names from parent modules.
1992 ///
1993 /// ### Example
1994 ///
1995 /// ```rust,ignore (proc-macro)
1996 /// // foo.rs
1997 /// #![crate_type = "proc-macro"]
1998 ///
1999 /// extern crate proc_macro;
2000 ///
2001 /// use proc_macro::*;
2002 ///
2003 /// #[proc_macro_derive(Foo)]
2004 /// pub fn foo1(a: TokenStream) -> TokenStream {
2005 /// drop(a);
2006 /// "mod __bar { static mut BAR: Option<Something> = None; }".parse().unwrap()
2007 /// }
2008 /// ```
2009 ///
2010 /// ```rust,ignore (needs-dependency)
2011 /// // bar.rs
2012 /// #[macro_use]
2013 /// extern crate foo;
2014 ///
2015 /// struct Something;
2016 ///
2017 /// #[derive(Foo)]
2018 /// struct Another;
2019 ///
2020 /// fn main() {}
2021 /// ```
2022 ///
2023 /// This will produce:
2024 ///
2025 /// ```text
2026 /// warning: cannot find type `Something` in this scope
2027 /// --> src/main.rs:8:10
2028 /// |
2029 /// 8 | #[derive(Foo)]
2030 /// | ^^^ names from parent modules are not accessible without an explicit import
2031 /// |
2032 /// = note: `#[warn(proc_macro_derive_resolution_fallback)]` on by default
2033 /// = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2034 /// = note: for more information, see issue #50504 <https://github.com/rust-lang/rust/issues/50504>
2035 /// ```
2036 ///
2037 /// ### Explanation
2038 ///
2039 /// If a proc-macro generates a module, the compiler unintentionally
2040 /// allowed items in that module to refer to items in the crate root
2041 /// without importing them. This is a [future-incompatible] lint to
2042 /// transition this to a hard error in the future. See [issue #50504] for
2043 /// more details.
2044 ///
2045 /// [issue #50504]: https://github.com/rust-lang/rust/issues/50504
2046 /// [future-incompatible]: ../index.md#future-incompatible-lints
2047 pub PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
2048 Deny,
2049 "detects proc macro derives using inaccessible names from parent modules",
2050 @future_incompatible = FutureIncompatibleInfo {
2051 reason: FutureIncompatibilityReason::FutureReleaseError,
2052 reference: "issue #83583 <https://github.com/rust-lang/rust/issues/83583>",
2053 report_in_deps: true,
2054 };
2055}
2056
2057declare_lint! {
2058 /// The `macro_use_extern_crate` lint detects the use of the [`macro_use` attribute].
2059 ///
2060 /// ### Example
2061 ///
2062 /// ```rust,ignore (needs extern crate)
2063 /// #![deny(macro_use_extern_crate)]
2064 ///
2065 /// #[macro_use]
2066 /// extern crate serde_json;
2067 ///
2068 /// fn main() {
2069 /// let _ = json!{{}};
2070 /// }
2071 /// ```
2072 ///
2073 /// This will produce:
2074 ///
2075 /// ```text
2076 /// error: applying the `#[macro_use]` attribute to an `extern crate` item is deprecated
2077 /// --> src/main.rs:3:1
2078 /// |
2079 /// 3 | #[macro_use]
2080 /// | ^^^^^^^^^^^^
2081 /// |
2082 /// = help: remove it and import macros at use sites with a `use` item instead
2083 /// note: the lint level is defined here
2084 /// --> src/main.rs:1:9
2085 /// |
2086 /// 1 | #![deny(macro_use_extern_crate)]
2087 /// | ^^^^^^^^^^^^^^^^^^^^^^
2088 /// ```
2089 ///
2090 /// ### Explanation
2091 ///
2092 /// The [`macro_use` attribute] on an [`extern crate`] item causes
2093 /// macros in that external crate to be brought into the prelude of the
2094 /// crate, making the macros in scope everywhere. As part of the efforts
2095 /// to simplify handling of dependencies in the [2018 edition], the use of
2096 /// `extern crate` is being phased out. To bring macros from extern crates
2097 /// into scope, it is recommended to use a [`use` import].
2098 ///
2099 /// This lint is "allow" by default because this is a stylistic choice
2100 /// that has not been settled, see [issue #52043] for more information.
2101 ///
2102 /// [`macro_use` attribute]: https://doc.rust-lang.org/reference/macros-by-example.html#the-macro_use-attribute
2103 /// [`use` import]: https://doc.rust-lang.org/reference/items/use-declarations.html
2104 /// [issue #52043]: https://github.com/rust-lang/rust/issues/52043
2105 pub MACRO_USE_EXTERN_CRATE,
2106 Allow,
2107 "the `#[macro_use]` attribute is now deprecated in favor of using macros \
2108 via the module system"
2109}
2110
2111declare_lint! {
2112 /// The `macro_expanded_macro_exports_accessed_by_absolute_paths` lint
2113 /// detects macro-expanded [`macro_export`] macros from the current crate
2114 /// that cannot be referred to by absolute paths.
2115 ///
2116 /// [`macro_export`]: https://doc.rust-lang.org/reference/macros-by-example.html#path-based-scope
2117 ///
2118 /// ### Example
2119 ///
2120 /// ```rust,compile_fail
2121 /// macro_rules! define_exported {
2122 /// () => {
2123 /// #[macro_export]
2124 /// macro_rules! exported {
2125 /// () => {};
2126 /// }
2127 /// };
2128 /// }
2129 ///
2130 /// define_exported!();
2131 ///
2132 /// fn main() {
2133 /// crate::exported!();
2134 /// }
2135 /// ```
2136 ///
2137 /// {{produces}}
2138 ///
2139 /// ### Explanation
2140 ///
2141 /// The intent is that all macros marked with the `#[macro_export]`
2142 /// attribute are made available in the root of the crate. However, when a
2143 /// `macro_rules!` definition is generated by another macro, the macro
2144 /// expansion is unable to uphold this rule. This is a
2145 /// [future-incompatible] lint to transition this to a hard error in the
2146 /// future. See [issue #53495] for more details.
2147 ///
2148 /// [issue #53495]: https://github.com/rust-lang/rust/issues/53495
2149 /// [future-incompatible]: ../index.md#future-incompatible-lints
2150 pub MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
2151 Deny,
2152 "macro-expanded `macro_export` macros from the current crate \
2153 cannot be referred to by absolute paths",
2154 @future_incompatible = FutureIncompatibleInfo {
2155 reason: FutureIncompatibilityReason::FutureReleaseError,
2156 reference: "issue #52234 <https://github.com/rust-lang/rust/issues/52234>",
2157 };
2158 crate_level_only
2159}
2160
2161declare_lint! {
2162 /// The `explicit_outlives_requirements` lint detects unnecessary
2163 /// lifetime bounds that can be inferred.
2164 ///
2165 /// ### Example
2166 ///
2167 /// ```rust,compile_fail
2168 /// # #![allow(unused)]
2169 /// #![deny(explicit_outlives_requirements)]
2170 /// #![deny(warnings)]
2171 ///
2172 /// struct SharedRef<'a, T>
2173 /// where
2174 /// T: 'a,
2175 /// {
2176 /// data: &'a T,
2177 /// }
2178 /// ```
2179 ///
2180 /// {{produces}}
2181 ///
2182 /// ### Explanation
2183 ///
2184 /// If a `struct` contains a reference, such as `&'a T`, the compiler
2185 /// requires that `T` outlives the lifetime `'a`. This historically
2186 /// required writing an explicit lifetime bound to indicate this
2187 /// requirement. However, this can be overly explicit, causing clutter and
2188 /// unnecessary complexity. The language was changed to automatically
2189 /// infer the bound if it is not specified. Specifically, if the struct
2190 /// contains a reference, directly or indirectly, to `T` with lifetime
2191 /// `'x`, then it will infer that `T: 'x` is a requirement.
2192 ///
2193 /// This lint is "allow" by default because it can be noisy for existing
2194 /// code that already had these requirements. This is a stylistic choice,
2195 /// as it is still valid to explicitly state the bound. It also has some
2196 /// false positives that can cause confusion.
2197 ///
2198 /// See [RFC 2093] for more details.
2199 ///
2200 /// [RFC 2093]: https://github.com/rust-lang/rfcs/blob/master/text/2093-infer-outlives.md
2201 pub EXPLICIT_OUTLIVES_REQUIREMENTS,
2202 Allow,
2203 "outlives requirements can be inferred"
2204}
2205
2206declare_lint! {
2207 /// The `deprecated_in_future` lint is internal to rustc and should not be
2208 /// used by user code.
2209 ///
2210 /// This lint is only enabled in the standard library. It works with the
2211 /// use of `#[deprecated]` with a `since` field of a version in the future.
2212 /// This allows something to be marked as deprecated in a future version,
2213 /// and then this lint will ensure that the item is no longer used in the
2214 /// standard library. See the [stability documentation] for more details.
2215 ///
2216 /// [stability documentation]: https://rustc-dev-guide.rust-lang.org/stability.html#deprecated
2217 pub DEPRECATED_IN_FUTURE,
2218 Allow,
2219 "detects use of items that will be deprecated in a future version",
2220 report_in_external_macro
2221}
2222
2223declare_lint! {
2224 /// The `ambiguous_associated_items` lint detects ambiguity between
2225 /// [associated items] and [enum variants].
2226 ///
2227 /// [associated items]: https://doc.rust-lang.org/reference/items/associated-items.html
2228 /// [enum variants]: https://doc.rust-lang.org/reference/items/enumerations.html
2229 ///
2230 /// ### Example
2231 ///
2232 /// ```rust,compile_fail
2233 /// enum E {
2234 /// V
2235 /// }
2236 ///
2237 /// trait Tr {
2238 /// type V;
2239 /// fn foo() -> Self::V;
2240 /// }
2241 ///
2242 /// impl Tr for E {
2243 /// type V = u8;
2244 /// // `Self::V` is ambiguous because it may refer to the associated type or
2245 /// // the enum variant.
2246 /// fn foo() -> Self::V { 0 }
2247 /// }
2248 /// ```
2249 ///
2250 /// {{produces}}
2251 ///
2252 /// ### Explanation
2253 ///
2254 /// Previous versions of Rust did not allow accessing enum variants
2255 /// through [type aliases]. When this ability was added (see [RFC 2338]), this
2256 /// introduced some situations where it can be ambiguous what a type
2257 /// was referring to.
2258 ///
2259 /// To fix this ambiguity, you should use a [qualified path] to explicitly
2260 /// state which type to use. For example, in the above example the
2261 /// function can be written as `fn f() -> <Self as Tr>::V { 0 }` to
2262 /// specifically refer to the associated type.
2263 ///
2264 /// This is a [future-incompatible] lint to transition this to a hard
2265 /// error in the future. See [issue #57644] for more details.
2266 ///
2267 /// [issue #57644]: https://github.com/rust-lang/rust/issues/57644
2268 /// [type aliases]: https://doc.rust-lang.org/reference/items/type-aliases.html#type-aliases
2269 /// [RFC 2338]: https://github.com/rust-lang/rfcs/blob/master/text/2338-type-alias-enum-variants.md
2270 /// [qualified path]: https://doc.rust-lang.org/reference/paths.html#qualified-paths
2271 /// [future-incompatible]: ../index.md#future-incompatible-lints
2272 pub AMBIGUOUS_ASSOCIATED_ITEMS,
2273 Deny,
2274 "ambiguous associated items",
2275 @future_incompatible = FutureIncompatibleInfo {
2276 reason: FutureIncompatibilityReason::FutureReleaseError,
2277 reference: "issue #57644 <https://github.com/rust-lang/rust/issues/57644>",
2278 };
2279}
2280
2281declare_lint! {
2282 /// The `soft_unstable` lint detects unstable features that were unintentionally allowed on
2283 /// stable. This is a [future-incompatible] lint to transition this to a hard error in the
2284 /// future. See [issue #64266] for more details.
2285 ///
2286 /// [issue #64266]: https://github.com/rust-lang/rust/issues/64266
2287 /// [future-incompatible]: ../index.md#future-incompatible-lints
2288 pub SOFT_UNSTABLE,
2289 Deny,
2290 "a feature gate that doesn't break dependent crates",
2291 @future_incompatible = FutureIncompatibleInfo {
2292 reason: FutureIncompatibilityReason::FutureReleaseError,
2293 reference: "issue #64266 <https://github.com/rust-lang/rust/issues/64266>",
2294 report_in_deps: true,
2295 };
2296}
2297
2298declare_lint! {
2299 /// The `inline_no_sanitize` lint detects incompatible use of
2300 /// [`#[inline(always)]`][inline] and [`#[no_sanitize(...)]`][no_sanitize].
2301 ///
2302 /// [inline]: https://doc.rust-lang.org/reference/attributes/codegen.html#the-inline-attribute
2303 /// [no_sanitize]: https://doc.rust-lang.org/nightly/unstable-book/language-features/no-sanitize.html
2304 ///
2305 /// ### Example
2306 ///
2307 /// ```rust
2308 /// #![feature(no_sanitize)]
2309 ///
2310 /// #[inline(always)]
2311 /// #[no_sanitize(address)]
2312 /// fn x() {}
2313 ///
2314 /// fn main() {
2315 /// x()
2316 /// }
2317 /// ```
2318 ///
2319 /// {{produces}}
2320 ///
2321 /// ### Explanation
2322 ///
2323 /// The use of the [`#[inline(always)]`][inline] attribute prevents the
2324 /// the [`#[no_sanitize(...)]`][no_sanitize] attribute from working.
2325 /// Consider temporarily removing `inline` attribute.
2326 pub INLINE_NO_SANITIZE,
2327 Warn,
2328 "detects incompatible use of `#[inline(always)]` and `#[no_sanitize(...)]`",
2329}
2330
2331declare_lint! {
2332 /// The `asm_sub_register` lint detects using only a subset of a register
2333 /// for inline asm inputs.
2334 ///
2335 /// ### Example
2336 ///
2337 /// ```rust,ignore (fails on non-x86_64)
2338 /// #[cfg(target_arch="x86_64")]
2339 /// use std::arch::asm;
2340 ///
2341 /// fn main() {
2342 /// #[cfg(target_arch="x86_64")]
2343 /// unsafe {
2344 /// asm!("mov {0}, {0}", in(reg) 0i16);
2345 /// }
2346 /// }
2347 /// ```
2348 ///
2349 /// This will produce:
2350 ///
2351 /// ```text
2352 /// warning: formatting may not be suitable for sub-register argument
2353 /// --> src/main.rs:7:19
2354 /// |
2355 /// 7 | asm!("mov {0}, {0}", in(reg) 0i16);
2356 /// | ^^^ ^^^ ---- for this argument
2357 /// |
2358 /// = note: `#[warn(asm_sub_register)]` on by default
2359 /// = help: use the `x` modifier to have the register formatted as `ax`
2360 /// = help: or use the `r` modifier to keep the default formatting of `rax`
2361 /// ```
2362 ///
2363 /// ### Explanation
2364 ///
2365 /// Registers on some architectures can use different names to refer to a
2366 /// subset of the register. By default, the compiler will use the name for
2367 /// the full register size. To explicitly use a subset of the register,
2368 /// you can override the default by using a modifier on the template
2369 /// string operand to specify when subregister to use. This lint is issued
2370 /// if you pass in a value with a smaller data type than the default
2371 /// register size, to alert you of possibly using the incorrect width. To
2372 /// fix this, add the suggested modifier to the template, or cast the
2373 /// value to the correct size.
2374 ///
2375 /// See [register template modifiers] in the reference for more details.
2376 ///
2377 /// [register template modifiers]: https://doc.rust-lang.org/nightly/reference/inline-assembly.html#template-modifiers
2378 pub ASM_SUB_REGISTER,
2379 Warn,
2380 "using only a subset of a register for inline asm inputs",
2381}
2382
2383declare_lint! {
2384 /// The `bad_asm_style` lint detects the use of the `.intel_syntax` and
2385 /// `.att_syntax` directives.
2386 ///
2387 /// ### Example
2388 ///
2389 /// ```rust,ignore (fails on non-x86_64)
2390 /// #[cfg(target_arch="x86_64")]
2391 /// use std::arch::asm;
2392 ///
2393 /// fn main() {
2394 /// #[cfg(target_arch="x86_64")]
2395 /// unsafe {
2396 /// asm!(
2397 /// ".att_syntax",
2398 /// "movq %{0}, %{0}", in(reg) 0usize
2399 /// );
2400 /// }
2401 /// }
2402 /// ```
2403 ///
2404 /// This will produce:
2405 ///
2406 /// ```text
2407 /// warning: avoid using `.att_syntax`, prefer using `options(att_syntax)` instead
2408 /// --> src/main.rs:8:14
2409 /// |
2410 /// 8 | ".att_syntax",
2411 /// | ^^^^^^^^^^^
2412 /// |
2413 /// = note: `#[warn(bad_asm_style)]` on by default
2414 /// ```
2415 ///
2416 /// ### Explanation
2417 ///
2418 /// On x86, `asm!` uses the intel assembly syntax by default. While this
2419 /// can be switched using assembler directives like `.att_syntax`, using the
2420 /// `att_syntax` option is recommended instead because it will also properly
2421 /// prefix register placeholders with `%` as required by AT&T syntax.
2422 pub BAD_ASM_STYLE,
2423 Warn,
2424 "incorrect use of inline assembly",
2425}
2426
2427declare_lint! {
2428 /// The `unsafe_op_in_unsafe_fn` lint detects unsafe operations in unsafe
2429 /// functions without an explicit unsafe block.
2430 ///
2431 /// ### Example
2432 ///
2433 /// ```rust,compile_fail
2434 /// #![deny(unsafe_op_in_unsafe_fn)]
2435 ///
2436 /// unsafe fn foo() {}
2437 ///
2438 /// unsafe fn bar() {
2439 /// foo();
2440 /// }
2441 ///
2442 /// fn main() {}
2443 /// ```
2444 ///
2445 /// {{produces}}
2446 ///
2447 /// ### Explanation
2448 ///
2449 /// Currently, an [`unsafe fn`] allows any [unsafe] operation within its
2450 /// body. However, this can increase the surface area of code that needs
2451 /// to be scrutinized for proper behavior. The [`unsafe` block] provides a
2452 /// convenient way to make it clear exactly which parts of the code are
2453 /// performing unsafe operations. In the future, it is desired to change
2454 /// it so that unsafe operations cannot be performed in an `unsafe fn`
2455 /// without an `unsafe` block.
2456 ///
2457 /// The fix to this is to wrap the unsafe code in an `unsafe` block.
2458 ///
2459 /// This lint is "allow" by default on editions up to 2021, from 2024 it is
2460 /// "warn" by default; the plan for increasing severity further is
2461 /// still being considered. See [RFC #2585] and [issue #71668] for more
2462 /// details.
2463 ///
2464 /// [`unsafe fn`]: https://doc.rust-lang.org/reference/unsafe-functions.html
2465 /// [`unsafe` block]: https://doc.rust-lang.org/reference/expressions/block-expr.html#unsafe-blocks
2466 /// [unsafe]: https://doc.rust-lang.org/reference/unsafety.html
2467 /// [RFC #2585]: https://github.com/rust-lang/rfcs/blob/master/text/2585-unsafe-block-in-unsafe-fn.md
2468 /// [issue #71668]: https://github.com/rust-lang/rust/issues/71668
2469 pub UNSAFE_OP_IN_UNSAFE_FN,
2470 Allow,
2471 "unsafe operations in unsafe functions without an explicit unsafe block are deprecated",
2472 @future_incompatible = FutureIncompatibleInfo {
2473 reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024),
2474 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-op-in-unsafe-fn.html>",
2475 explain_reason: false
2476 };
2477 @edition Edition2024 => Warn;
2478}
2479
2480declare_lint! {
2481 /// The `fuzzy_provenance_casts` lint detects an `as` cast between an integer
2482 /// and a pointer.
2483 ///
2484 /// ### Example
2485 ///
2486 /// ```rust
2487 /// #![feature(strict_provenance_lints)]
2488 /// #![warn(fuzzy_provenance_casts)]
2489 ///
2490 /// fn main() {
2491 /// let _dangling = 16_usize as *const u8;
2492 /// }
2493 /// ```
2494 ///
2495 /// {{produces}}
2496 ///
2497 /// ### Explanation
2498 ///
2499 /// This lint is part of the strict provenance effort, see [issue #95228].
2500 /// Casting an integer to a pointer is considered bad style, as a pointer
2501 /// contains, besides the *address* also a *provenance*, indicating what
2502 /// memory the pointer is allowed to read/write. Casting an integer, which
2503 /// doesn't have provenance, to a pointer requires the compiler to assign
2504 /// (guess) provenance. The compiler assigns "all exposed valid" (see the
2505 /// docs of [`ptr::with_exposed_provenance`] for more information about this
2506 /// "exposing"). This penalizes the optimiser and is not well suited for
2507 /// dynamic analysis/dynamic program verification (e.g. Miri or CHERI
2508 /// platforms).
2509 ///
2510 /// It is much better to use [`ptr::with_addr`] instead to specify the
2511 /// provenance you want. If using this function is not possible because the
2512 /// code relies on exposed provenance then there is as an escape hatch
2513 /// [`ptr::with_exposed_provenance`].
2514 ///
2515 /// [issue #95228]: https://github.com/rust-lang/rust/issues/95228
2516 /// [`ptr::with_addr`]: https://doc.rust-lang.org/core/primitive.pointer.html#method.with_addr
2517 /// [`ptr::with_exposed_provenance`]: https://doc.rust-lang.org/core/ptr/fn.with_exposed_provenance.html
2518 pub FUZZY_PROVENANCE_CASTS,
2519 Allow,
2520 "a fuzzy integer to pointer cast is used",
2521 @feature_gate = strict_provenance_lints;
2522}
2523
2524declare_lint! {
2525 /// The `lossy_provenance_casts` lint detects an `as` cast between a pointer
2526 /// and an integer.
2527 ///
2528 /// ### Example
2529 ///
2530 /// ```rust
2531 /// #![feature(strict_provenance_lints)]
2532 /// #![warn(lossy_provenance_casts)]
2533 ///
2534 /// fn main() {
2535 /// let x: u8 = 37;
2536 /// let _addr: usize = &x as *const u8 as usize;
2537 /// }
2538 /// ```
2539 ///
2540 /// {{produces}}
2541 ///
2542 /// ### Explanation
2543 ///
2544 /// This lint is part of the strict provenance effort, see [issue #95228].
2545 /// Casting a pointer to an integer is a lossy operation, because beyond
2546 /// just an *address* a pointer may be associated with a particular
2547 /// *provenance*. This information is used by the optimiser and for dynamic
2548 /// analysis/dynamic program verification (e.g. Miri or CHERI platforms).
2549 ///
2550 /// Since this cast is lossy, it is considered good style to use the
2551 /// [`ptr::addr`] method instead, which has a similar effect, but doesn't
2552 /// "expose" the pointer provenance. This improves optimisation potential.
2553 /// See the docs of [`ptr::addr`] and [`ptr::expose_provenance`] for more information
2554 /// about exposing pointer provenance.
2555 ///
2556 /// If your code can't comply with strict provenance and needs to expose
2557 /// the provenance, then there is [`ptr::expose_provenance`] as an escape hatch,
2558 /// which preserves the behaviour of `as usize` casts while being explicit
2559 /// about the semantics.
2560 ///
2561 /// [issue #95228]: https://github.com/rust-lang/rust/issues/95228
2562 /// [`ptr::addr`]: https://doc.rust-lang.org/core/primitive.pointer.html#method.addr
2563 /// [`ptr::expose_provenance`]: https://doc.rust-lang.org/core/primitive.pointer.html#method.expose_provenance
2564 pub LOSSY_PROVENANCE_CASTS,
2565 Allow,
2566 "a lossy pointer to integer cast is used",
2567 @feature_gate = strict_provenance_lints;
2568}
2569
2570declare_lint! {
2571 /// The `const_evaluatable_unchecked` lint detects a generic constant used
2572 /// in a type.
2573 ///
2574 /// ### Example
2575 ///
2576 /// ```rust
2577 /// const fn foo<T>() -> usize {
2578 /// if size_of::<*mut T>() < 8 { // size of *mut T does not depend on T
2579 /// 4
2580 /// } else {
2581 /// 8
2582 /// }
2583 /// }
2584 ///
2585 /// fn test<T>() {
2586 /// let _ = [0; foo::<T>()];
2587 /// }
2588 /// ```
2589 ///
2590 /// {{produces}}
2591 ///
2592 /// ### Explanation
2593 ///
2594 /// In the 1.43 release, some uses of generic parameters in array repeat
2595 /// expressions were accidentally allowed. This is a [future-incompatible]
2596 /// lint to transition this to a hard error in the future. See [issue
2597 /// #76200] for a more detailed description and possible fixes.
2598 ///
2599 /// [future-incompatible]: ../index.md#future-incompatible-lints
2600 /// [issue #76200]: https://github.com/rust-lang/rust/issues/76200
2601 pub CONST_EVALUATABLE_UNCHECKED,
2602 Warn,
2603 "detects a generic constant is used in a type without a emitting a warning",
2604 @future_incompatible = FutureIncompatibleInfo {
2605 reason: FutureIncompatibilityReason::FutureReleaseError,
2606 reference: "issue #76200 <https://github.com/rust-lang/rust/issues/76200>",
2607 };
2608}
2609
2610declare_lint! {
2611 /// The `function_item_references` lint detects function references that are
2612 /// formatted with [`fmt::Pointer`] or transmuted.
2613 ///
2614 /// [`fmt::Pointer`]: https://doc.rust-lang.org/std/fmt/trait.Pointer.html
2615 ///
2616 /// ### Example
2617 ///
2618 /// ```rust
2619 /// fn foo() { }
2620 ///
2621 /// fn main() {
2622 /// println!("{:p}", &foo);
2623 /// }
2624 /// ```
2625 ///
2626 /// {{produces}}
2627 ///
2628 /// ### Explanation
2629 ///
2630 /// Taking a reference to a function may be mistaken as a way to obtain a
2631 /// pointer to that function. This can give unexpected results when
2632 /// formatting the reference as a pointer or transmuting it. This lint is
2633 /// issued when function references are formatted as pointers, passed as
2634 /// arguments bound by [`fmt::Pointer`] or transmuted.
2635 pub FUNCTION_ITEM_REFERENCES,
2636 Warn,
2637 "suggest casting to a function pointer when attempting to take references to function items",
2638}
2639
2640declare_lint! {
2641 /// The `uninhabited_static` lint detects uninhabited statics.
2642 ///
2643 /// ### Example
2644 ///
2645 /// ```rust
2646 /// enum Void {}
2647 /// unsafe extern {
2648 /// static EXTERN: Void;
2649 /// }
2650 /// ```
2651 ///
2652 /// {{produces}}
2653 ///
2654 /// ### Explanation
2655 ///
2656 /// Statics with an uninhabited type can never be initialized, so they are impossible to define.
2657 /// However, this can be side-stepped with an `extern static`, leading to problems later in the
2658 /// compiler which assumes that there are no initialized uninhabited places (such as locals or
2659 /// statics). This was accidentally allowed, but is being phased out.
2660 pub UNINHABITED_STATIC,
2661 Warn,
2662 "uninhabited static",
2663 @future_incompatible = FutureIncompatibleInfo {
2664 reason: FutureIncompatibilityReason::FutureReleaseError,
2665 reference: "issue #74840 <https://github.com/rust-lang/rust/issues/74840>",
2666 };
2667}
2668
2669declare_lint! {
2670 /// The `unnameable_test_items` lint detects [`#[test]`][test] functions
2671 /// that are not able to be run by the test harness because they are in a
2672 /// position where they are not nameable.
2673 ///
2674 /// [test]: https://doc.rust-lang.org/reference/attributes/testing.html#the-test-attribute
2675 ///
2676 /// ### Example
2677 ///
2678 /// ```rust,test
2679 /// fn main() {
2680 /// #[test]
2681 /// fn foo() {
2682 /// // This test will not fail because it does not run.
2683 /// assert_eq!(1, 2);
2684 /// }
2685 /// }
2686 /// ```
2687 ///
2688 /// {{produces}}
2689 ///
2690 /// ### Explanation
2691 ///
2692 /// In order for the test harness to run a test, the test function must be
2693 /// located in a position where it can be accessed from the crate root.
2694 /// This generally means it must be defined in a module, and not anywhere
2695 /// else such as inside another function. The compiler previously allowed
2696 /// this without an error, so a lint was added as an alert that a test is
2697 /// not being used. Whether or not this should be allowed has not yet been
2698 /// decided, see [RFC 2471] and [issue #36629].
2699 ///
2700 /// [RFC 2471]: https://github.com/rust-lang/rfcs/pull/2471#issuecomment-397414443
2701 /// [issue #36629]: https://github.com/rust-lang/rust/issues/36629
2702 pub UNNAMEABLE_TEST_ITEMS,
2703 Warn,
2704 "detects an item that cannot be named being marked as `#[test_case]`",
2705 report_in_external_macro
2706}
2707
2708declare_lint! {
2709 /// The `useless_deprecated` lint detects deprecation attributes with no effect.
2710 ///
2711 /// ### Example
2712 ///
2713 /// ```rust,compile_fail
2714 /// struct X;
2715 ///
2716 /// #[deprecated = "message"]
2717 /// impl Default for X {
2718 /// fn default() -> Self {
2719 /// X
2720 /// }
2721 /// }
2722 /// ```
2723 ///
2724 /// {{produces}}
2725 ///
2726 /// ### Explanation
2727 ///
2728 /// Deprecation attributes have no effect on trait implementations.
2729 pub USELESS_DEPRECATED,
2730 Deny,
2731 "detects deprecation attributes with no effect",
2732}
2733
2734declare_lint! {
2735 /// The `ineffective_unstable_trait_impl` lint detects `#[unstable]` attributes which are not used.
2736 ///
2737 /// ### Example
2738 ///
2739 /// ```rust,compile_fail
2740 /// #![feature(staged_api)]
2741 ///
2742 /// #[derive(Clone)]
2743 /// #[stable(feature = "x", since = "1")]
2744 /// struct S {}
2745 ///
2746 /// #[unstable(feature = "y", issue = "none")]
2747 /// impl Copy for S {}
2748 /// ```
2749 ///
2750 /// {{produces}}
2751 ///
2752 /// ### Explanation
2753 ///
2754 /// `staged_api` does not currently support using a stability attribute on `impl` blocks.
2755 /// `impl`s are always stable if both the type and trait are stable, and always unstable otherwise.
2756 pub INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
2757 Deny,
2758 "detects `#[unstable]` on stable trait implementations for stable types"
2759}
2760
2761declare_lint! {
2762 /// The `self_constructor_from_outer_item` lint detects cases where the `Self` constructor
2763 /// was silently allowed due to a bug in the resolver, and which may produce surprising
2764 /// and unintended behavior.
2765 ///
2766 /// Using a `Self` type alias from an outer item was never intended, but was silently allowed.
2767 /// This is deprecated -- and is a hard error when the `Self` type alias references generics
2768 /// that are not in scope.
2769 ///
2770 /// ### Example
2771 ///
2772 /// ```rust,compile_fail
2773 /// #![deny(self_constructor_from_outer_item)]
2774 ///
2775 /// struct S0(usize);
2776 ///
2777 /// impl S0 {
2778 /// fn foo() {
2779 /// const C: S0 = Self(0);
2780 /// fn bar() -> S0 {
2781 /// Self(0)
2782 /// }
2783 /// }
2784 /// }
2785 /// ```
2786 ///
2787 /// {{produces}}
2788 ///
2789 /// ### Explanation
2790 ///
2791 /// The `Self` type alias should not be reachable because nested items are not associated with
2792 /// the scope of the parameters from the parent item.
2793 pub SELF_CONSTRUCTOR_FROM_OUTER_ITEM,
2794 Warn,
2795 "detect unsupported use of `Self` from outer item",
2796 @future_incompatible = FutureIncompatibleInfo {
2797 reason: FutureIncompatibilityReason::FutureReleaseError,
2798 reference: "issue #124186 <https://github.com/rust-lang/rust/issues/124186>",
2799 };
2800}
2801
2802declare_lint! {
2803 /// The `semicolon_in_expressions_from_macros` lint detects trailing semicolons
2804 /// in macro bodies when the macro is invoked in expression position.
2805 /// This was previous accepted, but is being phased out.
2806 ///
2807 /// ### Example
2808 ///
2809 /// ```rust,compile_fail
2810 /// #![deny(semicolon_in_expressions_from_macros)]
2811 /// macro_rules! foo {
2812 /// () => { true; }
2813 /// }
2814 ///
2815 /// fn main() {
2816 /// let val = match true {
2817 /// true => false,
2818 /// _ => foo!()
2819 /// };
2820 /// }
2821 /// ```
2822 ///
2823 /// {{produces}}
2824 ///
2825 /// ### Explanation
2826 ///
2827 /// Previous, Rust ignored trailing semicolon in a macro
2828 /// body when a macro was invoked in expression position.
2829 /// However, this makes the treatment of semicolons in the language
2830 /// inconsistent, and could lead to unexpected runtime behavior
2831 /// in some circumstances (e.g. if the macro author expects
2832 /// a value to be dropped).
2833 ///
2834 /// This is a [future-incompatible] lint to transition this
2835 /// to a hard error in the future. See [issue #79813] for more details.
2836 ///
2837 /// [issue #79813]: https://github.com/rust-lang/rust/issues/79813
2838 /// [future-incompatible]: ../index.md#future-incompatible-lints
2839 pub SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
2840 Warn,
2841 "trailing semicolon in macro body used as expression",
2842 @future_incompatible = FutureIncompatibleInfo {
2843 reason: FutureIncompatibilityReason::FutureReleaseError,
2844 reference: "issue #79813 <https://github.com/rust-lang/rust/issues/79813>",
2845 report_in_deps: true,
2846 };
2847}
2848
2849declare_lint! {
2850 /// The `legacy_derive_helpers` lint detects derive helper attributes
2851 /// that are used before they are introduced.
2852 ///
2853 /// ### Example
2854 ///
2855 /// ```rust,ignore (needs extern crate)
2856 /// #[serde(rename_all = "camelCase")]
2857 /// #[derive(Deserialize)]
2858 /// struct S { /* fields */ }
2859 /// ```
2860 ///
2861 /// produces:
2862 ///
2863 /// ```text
2864 /// warning: derive helper attribute is used before it is introduced
2865 /// --> $DIR/legacy-derive-helpers.rs:1:3
2866 /// |
2867 /// 1 | #[serde(rename_all = "camelCase")]
2868 /// | ^^^^^
2869 /// ...
2870 /// 2 | #[derive(Deserialize)]
2871 /// | ----------- the attribute is introduced here
2872 /// ```
2873 ///
2874 /// ### Explanation
2875 ///
2876 /// Attributes like this work for historical reasons, but attribute expansion works in
2877 /// left-to-right order in general, so, to resolve `#[serde]`, compiler has to try to "look
2878 /// into the future" at not yet expanded part of the item , but such attempts are not always
2879 /// reliable.
2880 ///
2881 /// To fix the warning place the helper attribute after its corresponding derive.
2882 /// ```rust,ignore (needs extern crate)
2883 /// #[derive(Deserialize)]
2884 /// #[serde(rename_all = "camelCase")]
2885 /// struct S { /* fields */ }
2886 /// ```
2887 pub LEGACY_DERIVE_HELPERS,
2888 Warn,
2889 "detects derive helper attributes that are used before they are introduced",
2890 @future_incompatible = FutureIncompatibleInfo {
2891 reason: FutureIncompatibilityReason::FutureReleaseError,
2892 reference: "issue #79202 <https://github.com/rust-lang/rust/issues/79202>",
2893 };
2894}
2895
2896declare_lint! {
2897 /// The `large_assignments` lint detects when objects of large
2898 /// types are being moved around.
2899 ///
2900 /// ### Example
2901 ///
2902 /// ```rust,ignore (can crash on some platforms)
2903 /// let x = [0; 50000];
2904 /// let y = x;
2905 /// ```
2906 ///
2907 /// produces:
2908 ///
2909 /// ```text
2910 /// warning: moving a large value
2911 /// --> $DIR/move-large.rs:1:3
2912 /// let y = x;
2913 /// - Copied large value here
2914 /// ```
2915 ///
2916 /// ### Explanation
2917 ///
2918 /// When using a large type in a plain assignment or in a function
2919 /// argument, idiomatic code can be inefficient.
2920 /// Ideally appropriate optimizations would resolve this, but such
2921 /// optimizations are only done in a best-effort manner.
2922 /// This lint will trigger on all sites of large moves and thus allow the
2923 /// user to resolve them in code.
2924 pub LARGE_ASSIGNMENTS,
2925 Warn,
2926 "detects large moves or copies",
2927}
2928
2929declare_lint! {
2930 /// The `unexpected_cfgs` lint detects unexpected conditional compilation conditions.
2931 ///
2932 /// ### Example
2933 ///
2934 /// ```text
2935 /// rustc --check-cfg 'cfg()'
2936 /// ```
2937 ///
2938 /// ```rust,ignore (needs command line option)
2939 /// #[cfg(widnows)]
2940 /// fn foo() {}
2941 /// ```
2942 ///
2943 /// This will produce:
2944 ///
2945 /// ```text
2946 /// warning: unexpected `cfg` condition name: `widnows`
2947 /// --> lint_example.rs:1:7
2948 /// |
2949 /// 1 | #[cfg(widnows)]
2950 /// | ^^^^^^^
2951 /// |
2952 /// = note: `#[warn(unexpected_cfgs)]` on by default
2953 /// ```
2954 ///
2955 /// ### Explanation
2956 ///
2957 /// This lint is only active when [`--check-cfg`][check-cfg] arguments are being
2958 /// passed to the compiler and triggers whenever an unexpected condition name or value is
2959 /// used.
2960 ///
2961 /// See the [Checking Conditional Configurations][check-cfg] section for more
2962 /// details.
2963 ///
2964 /// See the [Cargo Specifics][unexpected_cfgs_lint_config] section for configuring this lint in
2965 /// `Cargo.toml`.
2966 ///
2967 /// [check-cfg]: https://doc.rust-lang.org/nightly/rustc/check-cfg.html
2968 /// [unexpected_cfgs_lint_config]: https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html#check-cfg-in-lintsrust-table
2969 pub UNEXPECTED_CFGS,
2970 Warn,
2971 "detects unexpected names and values in `#[cfg]` conditions",
2972 report_in_external_macro
2973}
2974
2975declare_lint! {
2976 /// The `explicit_builtin_cfgs_in_flags` lint detects builtin cfgs set via the `--cfg` flag.
2977 ///
2978 /// ### Example
2979 ///
2980 /// ```text
2981 /// rustc --cfg unix
2982 /// ```
2983 ///
2984 /// ```rust,ignore (needs command line option)
2985 /// fn main() {}
2986 /// ```
2987 ///
2988 /// This will produce:
2989 ///
2990 /// ```text
2991 /// error: unexpected `--cfg unix` flag
2992 /// |
2993 /// = note: config `unix` is only supposed to be controlled by `--target`
2994 /// = note: manually setting a built-in cfg can and does create incoherent behaviors
2995 /// = note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default
2996 /// ```
2997 ///
2998 /// ### Explanation
2999 ///
3000 /// Setting builtin cfgs can and does produce incoherent behavior, it's better to the use
3001 /// the appropriate `rustc` flag that controls the config. For example setting the `windows`
3002 /// cfg but on Linux based target.
3003 pub EXPLICIT_BUILTIN_CFGS_IN_FLAGS,
3004 Deny,
3005 "detects builtin cfgs set via the `--cfg`"
3006}
3007
3008declare_lint! {
3009 /// The `repr_transparent_external_private_fields` lint
3010 /// detects types marked `#[repr(transparent)]` that (transitively)
3011 /// contain an external ZST type marked `#[non_exhaustive]` or containing
3012 /// private fields
3013 ///
3014 /// ### Example
3015 ///
3016 /// ```rust,ignore (needs external crate)
3017 /// #![deny(repr_transparent_external_private_fields)]
3018 /// use foo::NonExhaustiveZst;
3019 ///
3020 /// #[repr(transparent)]
3021 /// struct Bar(u32, ([u32; 0], NonExhaustiveZst));
3022 /// ```
3023 ///
3024 /// This will produce:
3025 ///
3026 /// ```text
3027 /// error: zero-sized fields in repr(transparent) cannot contain external non-exhaustive types
3028 /// --> src/main.rs:5:28
3029 /// |
3030 /// 5 | struct Bar(u32, ([u32; 0], NonExhaustiveZst));
3031 /// | ^^^^^^^^^^^^^^^^
3032 /// |
3033 /// note: the lint level is defined here
3034 /// --> src/main.rs:1:9
3035 /// |
3036 /// 1 | #![deny(repr_transparent_external_private_fields)]
3037 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3038 /// = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3039 /// = note: for more information, see issue #78586 <https://github.com/rust-lang/rust/issues/78586>
3040 /// = note: this struct contains `NonExhaustiveZst`, which is marked with `#[non_exhaustive]`, and makes it not a breaking change to become non-zero-sized in the future.
3041 /// ```
3042 ///
3043 /// ### Explanation
3044 ///
3045 /// Previous, Rust accepted fields that contain external private zero-sized types,
3046 /// even though it should not be a breaking change to add a non-zero-sized field to
3047 /// that private type.
3048 ///
3049 /// This is a [future-incompatible] lint to transition this
3050 /// to a hard error in the future. See [issue #78586] for more details.
3051 ///
3052 /// [issue #78586]: https://github.com/rust-lang/rust/issues/78586
3053 /// [future-incompatible]: ../index.md#future-incompatible-lints
3054 pub REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
3055 Warn,
3056 "transparent type contains an external ZST that is marked #[non_exhaustive] or contains private fields",
3057 @future_incompatible = FutureIncompatibleInfo {
3058 reason: FutureIncompatibilityReason::FutureReleaseError,
3059 reference: "issue #78586 <https://github.com/rust-lang/rust/issues/78586>",
3060 };
3061}
3062
3063declare_lint! {
3064 /// The `unstable_syntax_pre_expansion` lint detects the use of unstable
3065 /// syntax that is discarded during attribute expansion.
3066 ///
3067 /// ### Example
3068 ///
3069 /// ```rust
3070 /// #[cfg(FALSE)]
3071 /// macro foo() {}
3072 /// ```
3073 ///
3074 /// {{produces}}
3075 ///
3076 /// ### Explanation
3077 ///
3078 /// The input to active attributes such as `#[cfg]` or procedural macro
3079 /// attributes is required to be valid syntax. Previously, the compiler only
3080 /// gated the use of unstable syntax features after resolving `#[cfg]` gates
3081 /// and expanding procedural macros.
3082 ///
3083 /// To avoid relying on unstable syntax, move the use of unstable syntax
3084 /// into a position where the compiler does not parse the syntax, such as a
3085 /// functionlike macro.
3086 ///
3087 /// ```rust
3088 /// # #![deny(unstable_syntax_pre_expansion)]
3089 ///
3090 /// macro_rules! identity {
3091 /// ( $($tokens:tt)* ) => { $($tokens)* }
3092 /// }
3093 ///
3094 /// #[cfg(FALSE)]
3095 /// identity! {
3096 /// macro foo() {}
3097 /// }
3098 /// ```
3099 ///
3100 /// This is a [future-incompatible] lint to transition this
3101 /// to a hard error in the future. See [issue #65860] for more details.
3102 ///
3103 /// [issue #65860]: https://github.com/rust-lang/rust/issues/65860
3104 /// [future-incompatible]: ../index.md#future-incompatible-lints
3105 pub UNSTABLE_SYNTAX_PRE_EXPANSION,
3106 Warn,
3107 "unstable syntax can change at any point in the future, causing a hard error!",
3108 @future_incompatible = FutureIncompatibleInfo {
3109 reason: FutureIncompatibilityReason::FutureReleaseError,
3110 reference: "issue #65860 <https://github.com/rust-lang/rust/issues/65860>",
3111 };
3112}
3113
3114declare_lint! {
3115 /// The `ambiguous_glob_reexports` lint detects cases where names re-exported via globs
3116 /// collide. Downstream users trying to use the same name re-exported from multiple globs
3117 /// will receive a warning pointing out redefinition of the same name.
3118 ///
3119 /// ### Example
3120 ///
3121 /// ```rust,compile_fail
3122 /// #![deny(ambiguous_glob_reexports)]
3123 /// pub mod foo {
3124 /// pub type X = u8;
3125 /// }
3126 ///
3127 /// pub mod bar {
3128 /// pub type Y = u8;
3129 /// pub type X = u8;
3130 /// }
3131 ///
3132 /// pub use foo::*;
3133 /// pub use bar::*;
3134 ///
3135 ///
3136 /// pub fn main() {}
3137 /// ```
3138 ///
3139 /// {{produces}}
3140 ///
3141 /// ### Explanation
3142 ///
3143 /// This was previously accepted but it could silently break a crate's downstream users code.
3144 /// For example, if `foo::*` and `bar::*` were re-exported before `bar::X` was added to the
3145 /// re-exports, down stream users could use `this_crate::X` without problems. However, adding
3146 /// `bar::X` would cause compilation errors in downstream crates because `X` is defined
3147 /// multiple times in the same namespace of `this_crate`.
3148 pub AMBIGUOUS_GLOB_REEXPORTS,
3149 Warn,
3150 "ambiguous glob re-exports",
3151}
3152
3153declare_lint! {
3154 /// The `hidden_glob_reexports` lint detects cases where glob re-export items are shadowed by
3155 /// private items.
3156 ///
3157 /// ### Example
3158 ///
3159 /// ```rust,compile_fail
3160 /// #![deny(hidden_glob_reexports)]
3161 ///
3162 /// pub mod upstream {
3163 /// mod inner { pub struct Foo {}; pub struct Bar {}; }
3164 /// pub use self::inner::*;
3165 /// struct Foo {} // private item shadows `inner::Foo`
3166 /// }
3167 ///
3168 /// // mod downstream {
3169 /// // fn test() {
3170 /// // let _ = crate::upstream::Foo; // inaccessible
3171 /// // }
3172 /// // }
3173 ///
3174 /// pub fn main() {}
3175 /// ```
3176 ///
3177 /// {{produces}}
3178 ///
3179 /// ### Explanation
3180 ///
3181 /// This was previously accepted without any errors or warnings but it could silently break a
3182 /// crate's downstream user code. If the `struct Foo` was added, `dep::inner::Foo` would
3183 /// silently become inaccessible and trigger a "`struct `Foo` is private`" visibility error at
3184 /// the downstream use site.
3185 pub HIDDEN_GLOB_REEXPORTS,
3186 Warn,
3187 "name introduced by a private item shadows a name introduced by a public glob re-export",
3188}
3189
3190declare_lint! {
3191 /// The `long_running_const_eval` lint is emitted when const
3192 /// eval is running for a long time to ensure rustc terminates
3193 /// even if you accidentally wrote an infinite loop.
3194 ///
3195 /// ### Example
3196 ///
3197 /// ```rust,compile_fail
3198 /// const FOO: () = loop {};
3199 /// ```
3200 ///
3201 /// {{produces}}
3202 ///
3203 /// ### Explanation
3204 ///
3205 /// Loops allow const evaluation to compute arbitrary code, but may also
3206 /// cause infinite loops or just very long running computations.
3207 /// Users can enable long running computations by allowing the lint
3208 /// on individual constants or for entire crates.
3209 ///
3210 /// ### Unconditional warnings
3211 ///
3212 /// Note that regardless of whether the lint is allowed or set to warn,
3213 /// the compiler will issue warnings if constant evaluation runs significantly
3214 /// longer than this lint's limit. These warnings are also shown to downstream
3215 /// users from crates.io or similar registries. If you are above the lint's limit,
3216 /// both you and downstream users might be exposed to these warnings.
3217 /// They might also appear on compiler updates, as the compiler makes minor changes
3218 /// about how complexity is measured: staying below the limit ensures that there
3219 /// is enough room, and given that the lint is disabled for people who use your
3220 /// dependency it means you will be the only one to get the warning and can put
3221 /// out an update in your own time.
3222 pub LONG_RUNNING_CONST_EVAL,
3223 Deny,
3224 "detects long const eval operations",
3225 report_in_external_macro
3226}
3227
3228declare_lint! {
3229 /// The `unused_associated_type_bounds` lint is emitted when an
3230 /// associated type bound is added to a trait object, but the associated
3231 /// type has a `where Self: Sized` bound, and is thus unavailable on the
3232 /// trait object anyway.
3233 ///
3234 /// ### Example
3235 ///
3236 /// ```rust
3237 /// trait Foo {
3238 /// type Bar where Self: Sized;
3239 /// }
3240 /// type Mop = dyn Foo<Bar = ()>;
3241 /// ```
3242 ///
3243 /// {{produces}}
3244 ///
3245 /// ### Explanation
3246 ///
3247 /// Just like methods with `Self: Sized` bounds are unavailable on trait
3248 /// objects, associated types can be removed from the trait object.
3249 pub UNUSED_ASSOCIATED_TYPE_BOUNDS,
3250 Warn,
3251 "detects unused `Foo = Bar` bounds in `dyn Trait<Foo = Bar>`"
3252}
3253
3254declare_lint! {
3255 /// The `unused_doc_comments` lint detects doc comments that aren't used
3256 /// by `rustdoc`.
3257 ///
3258 /// ### Example
3259 ///
3260 /// ```rust
3261 /// /// docs for x
3262 /// let x = 12;
3263 /// ```
3264 ///
3265 /// {{produces}}
3266 ///
3267 /// ### Explanation
3268 ///
3269 /// `rustdoc` does not use doc comments in all positions, and so the doc
3270 /// comment will be ignored. Try changing it to a normal comment with `//`
3271 /// to avoid the warning.
3272 pub UNUSED_DOC_COMMENTS,
3273 Warn,
3274 "detects doc comments that aren't used by rustdoc"
3275}
3276
3277declare_lint! {
3278 /// The `rust_2021_incompatible_closure_captures` lint detects variables that aren't completely
3279 /// captured in Rust 2021, such that the `Drop` order of their fields may differ between
3280 /// Rust 2018 and 2021.
3281 ///
3282 /// It can also detect when a variable implements a trait like `Send`, but one of its fields does not,
3283 /// and the field is captured by a closure and used with the assumption that said field implements
3284 /// the same trait as the root variable.
3285 ///
3286 /// ### Example of drop reorder
3287 ///
3288 /// ```rust,edition2018,compile_fail
3289 /// #![deny(rust_2021_incompatible_closure_captures)]
3290 /// # #![allow(unused)]
3291 ///
3292 /// struct FancyInteger(i32);
3293 ///
3294 /// impl Drop for FancyInteger {
3295 /// fn drop(&mut self) {
3296 /// println!("Just dropped {}", self.0);
3297 /// }
3298 /// }
3299 ///
3300 /// struct Point { x: FancyInteger, y: FancyInteger }
3301 ///
3302 /// fn main() {
3303 /// let p = Point { x: FancyInteger(10), y: FancyInteger(20) };
3304 ///
3305 /// let c = || {
3306 /// let x = p.x;
3307 /// };
3308 ///
3309 /// c();
3310 ///
3311 /// // ... More code ...
3312 /// }
3313 /// ```
3314 ///
3315 /// {{produces}}
3316 ///
3317 /// ### Explanation
3318 ///
3319 /// In the above example, `p.y` will be dropped at the end of `f` instead of
3320 /// with `c` in Rust 2021.
3321 ///
3322 /// ### Example of auto-trait
3323 ///
3324 /// ```rust,edition2018,compile_fail
3325 /// #![deny(rust_2021_incompatible_closure_captures)]
3326 /// use std::thread;
3327 ///
3328 /// struct Pointer(*mut i32);
3329 /// unsafe impl Send for Pointer {}
3330 ///
3331 /// fn main() {
3332 /// let mut f = 10;
3333 /// let fptr = Pointer(&mut f as *mut i32);
3334 /// thread::spawn(move || unsafe {
3335 /// *fptr.0 = 20;
3336 /// });
3337 /// }
3338 /// ```
3339 ///
3340 /// {{produces}}
3341 ///
3342 /// ### Explanation
3343 ///
3344 /// In the above example, only `fptr.0` is captured in Rust 2021.
3345 /// The field is of type `*mut i32`, which doesn't implement `Send`,
3346 /// making the code invalid as the field cannot be sent between threads safely.
3347 pub RUST_2021_INCOMPATIBLE_CLOSURE_CAPTURES,
3348 Allow,
3349 "detects closures affected by Rust 2021 changes",
3350 @future_incompatible = FutureIncompatibleInfo {
3351 reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2021),
3352 explain_reason: false,
3353 };
3354}
3355
3356declare_lint_pass!(UnusedDocComment => [UNUSED_DOC_COMMENTS]);
3357
3358declare_lint! {
3359 /// The `missing_abi` lint detects cases where the ABI is omitted from
3360 /// `extern` declarations.
3361 ///
3362 /// ### Example
3363 ///
3364 /// ```rust,compile_fail
3365 /// #![deny(missing_abi)]
3366 ///
3367 /// extern fn foo() {}
3368 /// ```
3369 ///
3370 /// {{produces}}
3371 ///
3372 /// ### Explanation
3373 ///
3374 /// For historic reasons, Rust implicitly selects `C` as the default ABI for
3375 /// `extern` declarations. [Other ABIs] like `C-unwind` and `system` have
3376 /// been added since then, and especially with their addition seeing the ABI
3377 /// easily makes code review easier.
3378 ///
3379 /// [Other ABIs]: https://doc.rust-lang.org/reference/items/external-blocks.html#abi
3380 pub MISSING_ABI,
3381 Warn,
3382 "No declared ABI for extern declaration"
3383}
3384
3385declare_lint! {
3386 /// The `invalid_doc_attributes` lint detects when the `#[doc(...)]` is
3387 /// misused.
3388 ///
3389 /// ### Example
3390 ///
3391 /// ```rust,compile_fail
3392 /// #![deny(warnings)]
3393 ///
3394 /// pub mod submodule {
3395 /// #![doc(test(no_crate_inject))]
3396 /// }
3397 /// ```
3398 ///
3399 /// {{produces}}
3400 ///
3401 /// ### Explanation
3402 ///
3403 /// Previously, incorrect usage of the `#[doc(..)]` attribute was not
3404 /// being validated. Usually these should be rejected as a hard error,
3405 /// but this lint was introduced to avoid breaking any existing
3406 /// crates which included them.
3407 pub INVALID_DOC_ATTRIBUTES,
3408 Deny,
3409 "detects invalid `#[doc(...)]` attributes",
3410}
3411
3412declare_lint! {
3413 /// The `rust_2021_incompatible_or_patterns` lint detects usage of old versions of or-patterns.
3414 ///
3415 /// ### Example
3416 ///
3417 /// ```rust,edition2018,compile_fail
3418 /// #![deny(rust_2021_incompatible_or_patterns)]
3419 ///
3420 /// macro_rules! match_any {
3421 /// ( $expr:expr , $( $( $pat:pat )|+ => $expr_arm:expr ),+ ) => {
3422 /// match $expr {
3423 /// $(
3424 /// $( $pat => $expr_arm, )+
3425 /// )+
3426 /// }
3427 /// };
3428 /// }
3429 ///
3430 /// fn main() {
3431 /// let result: Result<i64, i32> = Err(42);
3432 /// let int: i64 = match_any!(result, Ok(i) | Err(i) => i.into());
3433 /// assert_eq!(int, 42);
3434 /// }
3435 /// ```
3436 ///
3437 /// {{produces}}
3438 ///
3439 /// ### Explanation
3440 ///
3441 /// In Rust 2021, the `pat` matcher will match additional patterns, which include the `|` character.
3442 pub RUST_2021_INCOMPATIBLE_OR_PATTERNS,
3443 Allow,
3444 "detects usage of old versions of or-patterns",
3445 @future_incompatible = FutureIncompatibleInfo {
3446 reason: FutureIncompatibilityReason::EditionError(Edition::Edition2021),
3447 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2021/or-patterns-macro-rules.html>",
3448 };
3449}
3450
3451declare_lint! {
3452 /// The `rust_2021_prelude_collisions` lint detects the usage of trait methods which are ambiguous
3453 /// with traits added to the prelude in future editions.
3454 ///
3455 /// ### Example
3456 ///
3457 /// ```rust,edition2018,compile_fail
3458 /// #![deny(rust_2021_prelude_collisions)]
3459 ///
3460 /// trait Foo {
3461 /// fn try_into(self) -> Result<String, !>;
3462 /// }
3463 ///
3464 /// impl Foo for &str {
3465 /// fn try_into(self) -> Result<String, !> {
3466 /// Ok(String::from(self))
3467 /// }
3468 /// }
3469 ///
3470 /// fn main() {
3471 /// let x: String = "3".try_into().unwrap();
3472 /// // ^^^^^^^^
3473 /// // This call to try_into matches both Foo::try_into and TryInto::try_into as
3474 /// // `TryInto` has been added to the Rust prelude in 2021 edition.
3475 /// println!("{x}");
3476 /// }
3477 /// ```
3478 ///
3479 /// {{produces}}
3480 ///
3481 /// ### Explanation
3482 ///
3483 /// In Rust 2021, one of the important introductions is the [prelude changes], which add
3484 /// `TryFrom`, `TryInto`, and `FromIterator` into the standard library's prelude. Since this
3485 /// results in an ambiguity as to which method/function to call when an existing `try_into`
3486 /// method is called via dot-call syntax or a `try_from`/`from_iter` associated function
3487 /// is called directly on a type.
3488 ///
3489 /// [prelude changes]: https://blog.rust-lang.org/inside-rust/2021/03/04/planning-rust-2021.html#prelude-changes
3490 pub RUST_2021_PRELUDE_COLLISIONS,
3491 Allow,
3492 "detects the usage of trait methods which are ambiguous with traits added to the \
3493 prelude in future editions",
3494 @future_incompatible = FutureIncompatibleInfo {
3495 reason: FutureIncompatibilityReason::EditionError(Edition::Edition2021),
3496 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2021/prelude.html>",
3497 };
3498}
3499
3500declare_lint! {
3501 /// The `rust_2024_prelude_collisions` lint detects the usage of trait methods which are ambiguous
3502 /// with traits added to the prelude in future editions.
3503 ///
3504 /// ### Example
3505 ///
3506 /// ```rust,edition2021,compile_fail
3507 /// #![deny(rust_2024_prelude_collisions)]
3508 /// trait Meow {
3509 /// fn poll(&self) {}
3510 /// }
3511 /// impl<T> Meow for T {}
3512 ///
3513 /// fn main() {
3514 /// core::pin::pin!(async {}).poll();
3515 /// // ^^^^^^
3516 /// // This call to try_into matches both Future::poll and Meow::poll as
3517 /// // `Future` has been added to the Rust prelude in 2024 edition.
3518 /// }
3519 /// ```
3520 ///
3521 /// {{produces}}
3522 ///
3523 /// ### Explanation
3524 ///
3525 /// Rust 2024, introduces two new additions to the standard library's prelude:
3526 /// `Future` and `IntoFuture`. This results in an ambiguity as to which method/function
3527 /// to call when an existing `poll`/`into_future` method is called via dot-call syntax or
3528 /// a `poll`/`into_future` associated function is called directly on a type.
3529 ///
3530 pub RUST_2024_PRELUDE_COLLISIONS,
3531 Allow,
3532 "detects the usage of trait methods which are ambiguous with traits added to the \
3533 prelude in future editions",
3534 @future_incompatible = FutureIncompatibleInfo {
3535 reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024),
3536 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/prelude.html>",
3537 };
3538}
3539
3540declare_lint! {
3541 /// The `rust_2021_prefixes_incompatible_syntax` lint detects identifiers that will be parsed as a
3542 /// prefix instead in Rust 2021.
3543 ///
3544 /// ### Example
3545 ///
3546 /// ```rust,edition2018,compile_fail
3547 /// #![deny(rust_2021_prefixes_incompatible_syntax)]
3548 ///
3549 /// macro_rules! m {
3550 /// (z $x:expr) => ();
3551 /// }
3552 ///
3553 /// m!(z"hey");
3554 /// ```
3555 ///
3556 /// {{produces}}
3557 ///
3558 /// ### Explanation
3559 ///
3560 /// In Rust 2015 and 2018, `z"hey"` is two tokens: the identifier `z`
3561 /// followed by the string literal `"hey"`. In Rust 2021, the `z` is
3562 /// considered a prefix for `"hey"`.
3563 ///
3564 /// This lint suggests to add whitespace between the `z` and `"hey"` tokens
3565 /// to keep them separated in Rust 2021.
3566 // Allow this lint -- rustdoc doesn't yet support threading edition into this lint's parser.
3567 #[allow(rustdoc::invalid_rust_codeblocks)]
3568 pub RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX,
3569 Allow,
3570 "identifiers that will be parsed as a prefix in Rust 2021",
3571 @future_incompatible = FutureIncompatibleInfo {
3572 reason: FutureIncompatibilityReason::EditionError(Edition::Edition2021),
3573 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2021/reserving-syntax.html>",
3574 };
3575 crate_level_only
3576}
3577
3578declare_lint! {
3579 /// The `unsupported_calling_conventions` lint is output whenever there is a use of the
3580 /// `stdcall`, `fastcall`, and `cdecl` calling conventions (or their unwind
3581 /// variants) on targets that cannot meaningfully be supported for the requested target.
3582 ///
3583 /// For example `stdcall` does not make much sense for a x86_64 or, more apparently, powerpc
3584 /// code, because this calling convention was never specified for those targets.
3585 ///
3586 /// Historically MSVC toolchains have fallen back to the regular C calling convention for
3587 /// targets other than x86, but Rust doesn't really see a similar need to introduce a similar
3588 /// hack across many more targets.
3589 ///
3590 /// ### Example
3591 ///
3592 /// ```rust,ignore (needs specific targets)
3593 /// extern "stdcall" fn stdcall() {}
3594 /// ```
3595 ///
3596 /// This will produce:
3597 ///
3598 /// ```text
3599 /// warning: use of calling convention not supported on this target
3600 /// --> $DIR/unsupported.rs:39:1
3601 /// |
3602 /// LL | extern "stdcall" fn stdcall() {}
3603 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3604 /// |
3605 /// = note: `#[warn(unsupported_calling_conventions)]` on by default
3606 /// = warning: this was previously accepted by the compiler but is being phased out;
3607 /// it will become a hard error in a future release!
3608 /// = note: for more information, see issue ...
3609 /// ```
3610 ///
3611 /// ### Explanation
3612 ///
3613 /// On most of the targets the behaviour of `stdcall` and similar calling conventions is not
3614 /// defined at all, but was previously accepted due to a bug in the implementation of the
3615 /// compiler.
3616 pub UNSUPPORTED_CALLING_CONVENTIONS,
3617 Warn,
3618 "use of unsupported calling convention",
3619 @future_incompatible = FutureIncompatibleInfo {
3620 reason: FutureIncompatibilityReason::FutureReleaseError,
3621 report_in_deps: false,
3622 reference: "issue #137018 <https://github.com/rust-lang/rust/issues/137018>",
3623 };
3624}
3625
3626declare_lint! {
3627 /// The `unsupported_fn_ptr_calling_conventions` lint is output whenever there is a use of
3628 /// a target dependent calling convention on a target that does not support this calling
3629 /// convention on a function pointer.
3630 ///
3631 /// For example `stdcall` does not make much sense for a x86_64 or, more apparently, powerpc
3632 /// code, because this calling convention was never specified for those targets.
3633 ///
3634 /// ### Example
3635 ///
3636 /// ```rust,ignore (needs specific targets)
3637 /// fn stdcall_ptr(f: extern "stdcall" fn ()) {
3638 /// f()
3639 /// }
3640 /// ```
3641 ///
3642 /// This will produce:
3643 ///
3644 /// ```text
3645 /// warning: the calling convention `"stdcall"` is not supported on this target
3646 /// --> $DIR/unsupported.rs:34:15
3647 /// |
3648 /// LL | fn stdcall_ptr(f: extern "stdcall" fn()) {
3649 /// | ^^^^^^^^^^^^^^^^^^^^^^^^
3650 /// |
3651 /// = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3652 /// = note: for more information, see issue #130260 <https://github.com/rust-lang/rust/issues/130260>
3653 /// = note: `#[warn(unsupported_fn_ptr_calling_conventions)]` on by default
3654 /// ```
3655 ///
3656 /// ### Explanation
3657 ///
3658 /// On most of the targets the behaviour of `stdcall` and similar calling conventions is not
3659 /// defined at all, but was previously accepted due to a bug in the implementation of the
3660 /// compiler.
3661 pub UNSUPPORTED_FN_PTR_CALLING_CONVENTIONS,
3662 Warn,
3663 "use of unsupported calling convention for function pointer",
3664 @future_incompatible = FutureIncompatibleInfo {
3665 reason: FutureIncompatibilityReason::FutureReleaseError,
3666 reference: "issue #130260 <https://github.com/rust-lang/rust/issues/130260>",
3667 report_in_deps: true,
3668 };
3669}
3670
3671declare_lint! {
3672 /// The `break_with_label_and_loop` lint detects labeled `break` expressions with
3673 /// an unlabeled loop as their value expression.
3674 ///
3675 /// ### Example
3676 ///
3677 /// ```rust
3678 /// 'label: loop {
3679 /// break 'label loop { break 42; };
3680 /// };
3681 /// ```
3682 ///
3683 /// {{produces}}
3684 ///
3685 /// ### Explanation
3686 ///
3687 /// In Rust, loops can have a label, and `break` expressions can refer to that label to
3688 /// break out of specific loops (and not necessarily the innermost one). `break` expressions
3689 /// can also carry a value expression, which can be another loop. A labeled `break` with an
3690 /// unlabeled loop as its value expression is easy to confuse with an unlabeled break with
3691 /// a labeled loop and is thus discouraged (but allowed for compatibility); use parentheses
3692 /// around the loop expression to silence this warning. Unlabeled `break` expressions with
3693 /// labeled loops yield a hard error, which can also be silenced by wrapping the expression
3694 /// in parentheses.
3695 pub BREAK_WITH_LABEL_AND_LOOP,
3696 Warn,
3697 "`break` expression with label and unlabeled loop as value expression"
3698}
3699
3700declare_lint! {
3701 /// The `non_exhaustive_omitted_patterns` lint aims to help consumers of a `#[non_exhaustive]`
3702 /// struct or enum who want to match all of its fields/variants explicitly.
3703 ///
3704 /// The `#[non_exhaustive]` annotation forces matches to use wildcards, so exhaustiveness
3705 /// checking cannot be used to ensure that all fields/variants are matched explicitly. To remedy
3706 /// this, this allow-by-default lint warns the user when a match mentions some but not all of
3707 /// the fields/variants of a `#[non_exhaustive]` struct or enum.
3708 ///
3709 /// ### Example
3710 ///
3711 /// ```rust,ignore (needs separate crate)
3712 /// // crate A
3713 /// #[non_exhaustive]
3714 /// pub enum Bar {
3715 /// A,
3716 /// B, // added variant in non breaking change
3717 /// }
3718 ///
3719 /// // in crate B
3720 /// #![feature(non_exhaustive_omitted_patterns_lint)]
3721 /// #[warn(non_exhaustive_omitted_patterns)]
3722 /// match Bar::A {
3723 /// Bar::A => {},
3724 /// _ => {},
3725 /// }
3726 /// ```
3727 ///
3728 /// This will produce:
3729 ///
3730 /// ```text
3731 /// warning: some variants are not matched explicitly
3732 /// --> $DIR/reachable-patterns.rs:70:9
3733 /// |
3734 /// LL | match Bar::A {
3735 /// | ^ pattern `Bar::B` not covered
3736 /// |
3737 /// note: the lint level is defined here
3738 /// --> $DIR/reachable-patterns.rs:69:16
3739 /// |
3740 /// LL | #[warn(non_exhaustive_omitted_patterns)]
3741 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3742 /// = help: ensure that all variants are matched explicitly by adding the suggested match arms
3743 /// = note: the matched value is of type `Bar` and the `non_exhaustive_omitted_patterns` attribute was found
3744 /// ```
3745 ///
3746 /// Warning: setting this to `deny` will make upstream non-breaking changes (adding fields or
3747 /// variants to a `#[non_exhaustive]` struct or enum) break your crate. This goes against
3748 /// expected semver behavior.
3749 ///
3750 /// ### Explanation
3751 ///
3752 /// Structs and enums tagged with `#[non_exhaustive]` force the user to add a (potentially
3753 /// redundant) wildcard when pattern-matching, to allow for future addition of fields or
3754 /// variants. The `non_exhaustive_omitted_patterns` lint detects when such a wildcard happens to
3755 /// actually catch some fields/variants. In other words, when the match without the wildcard
3756 /// would not be exhaustive. This lets the user be informed if new fields/variants were added.
3757 pub NON_EXHAUSTIVE_OMITTED_PATTERNS,
3758 Allow,
3759 "detect when patterns of types marked `non_exhaustive` are missed",
3760 @feature_gate = non_exhaustive_omitted_patterns_lint;
3761}
3762
3763declare_lint! {
3764 /// The `text_direction_codepoint_in_comment` lint detects Unicode codepoints in comments that
3765 /// change the visual representation of text on screen in a way that does not correspond to
3766 /// their on memory representation.
3767 ///
3768 /// ### Example
3769 ///
3770 /// ```rust,compile_fail
3771 /// #![deny(text_direction_codepoint_in_comment)]
3772 /// fn main() {
3773 #[doc = " println!(\"{:?}\"); // '\u{202E}');"]
3774 /// }
3775 /// ```
3776 ///
3777 /// {{produces}}
3778 ///
3779 /// ### Explanation
3780 ///
3781 /// Unicode allows changing the visual flow of text on screen in order to support scripts that
3782 /// are written right-to-left, but a specially crafted comment can make code that will be
3783 /// compiled appear to be part of a comment, depending on the software used to read the code.
3784 /// To avoid potential problems or confusion, such as in CVE-2021-42574, by default we deny
3785 /// their use.
3786 pub TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
3787 Deny,
3788 "invisible directionality-changing codepoints in comment",
3789 crate_level_only
3790}
3791
3792declare_lint! {
3793 /// The `text_direction_codepoint_in_literal` lint detects Unicode codepoints that change the
3794 /// visual representation of text on screen in a way that does not correspond to their on
3795 /// memory representation.
3796 ///
3797 /// ### Explanation
3798 ///
3799 /// The unicode characters `\u{202A}`, `\u{202B}`, `\u{202D}`, `\u{202E}`, `\u{2066}`,
3800 /// `\u{2067}`, `\u{2068}`, `\u{202C}` and `\u{2069}` make the flow of text on screen change
3801 /// its direction on software that supports these codepoints. This makes the text "abc" display
3802 /// as "cba" on screen. By leveraging software that supports these, people can write specially
3803 /// crafted literals that make the surrounding code seem like it's performing one action, when
3804 /// in reality it is performing another. Because of this, we proactively lint against their
3805 /// presence to avoid surprises.
3806 ///
3807 /// ### Example
3808 ///
3809 /// ```rust,compile_fail
3810 /// #![deny(text_direction_codepoint_in_literal)]
3811 /// fn main() {
3812 // ` - convince tidy that backticks match
3813 #[doc = " println!(\"{:?}\", '\u{202E}');"]
3814 // `
3815 /// }
3816 /// ```
3817 ///
3818 /// {{produces}}
3819 ///
3820 pub TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
3821 Deny,
3822 "detect special Unicode codepoints that affect the visual representation of text on screen, \
3823 changing the direction in which text flows",
3824 crate_level_only
3825}
3826
3827declare_lint! {
3828 /// The `duplicate_macro_attributes` lint detects when a `#[test]`-like built-in macro
3829 /// attribute is duplicated on an item. This lint may trigger on `bench`, `cfg_eval`, `test`
3830 /// and `test_case`.
3831 ///
3832 /// ### Example
3833 ///
3834 /// ```rust,ignore (needs --test)
3835 /// #[test]
3836 /// #[test]
3837 /// fn foo() {}
3838 /// ```
3839 ///
3840 /// This will produce:
3841 ///
3842 /// ```text
3843 /// warning: duplicated attribute
3844 /// --> src/lib.rs:2:1
3845 /// |
3846 /// 2 | #[test]
3847 /// | ^^^^^^^
3848 /// |
3849 /// = note: `#[warn(duplicate_macro_attributes)]` on by default
3850 /// ```
3851 ///
3852 /// ### Explanation
3853 ///
3854 /// A duplicated attribute may erroneously originate from a copy-paste and the effect of it
3855 /// being duplicated may not be obvious or desirable.
3856 ///
3857 /// For instance, doubling the `#[test]` attributes registers the test to be run twice with no
3858 /// change to its environment.
3859 ///
3860 /// [issue #90979]: https://github.com/rust-lang/rust/issues/90979
3861 pub DUPLICATE_MACRO_ATTRIBUTES,
3862 Warn,
3863 "duplicated attribute"
3864}
3865
3866declare_lint! {
3867 /// The `deprecated_where_clause_location` lint detects when a where clause in front of the equals
3868 /// in an associated type.
3869 ///
3870 /// ### Example
3871 ///
3872 /// ```rust
3873 /// trait Trait {
3874 /// type Assoc<'a> where Self: 'a;
3875 /// }
3876 ///
3877 /// impl Trait for () {
3878 /// type Assoc<'a> where Self: 'a = ();
3879 /// }
3880 /// ```
3881 ///
3882 /// {{produces}}
3883 ///
3884 /// ### Explanation
3885 ///
3886 /// The preferred location for where clauses on associated types
3887 /// is after the type. However, for most of generic associated types development,
3888 /// it was only accepted before the equals. To provide a transition period and
3889 /// further evaluate this change, both are currently accepted. At some point in
3890 /// the future, this may be disallowed at an edition boundary; but, that is
3891 /// undecided currently.
3892 pub DEPRECATED_WHERE_CLAUSE_LOCATION,
3893 Warn,
3894 "deprecated where clause location"
3895}
3896
3897declare_lint! {
3898 /// The `test_unstable_lint` lint tests unstable lints and is perma-unstable.
3899 ///
3900 /// ### Example
3901 ///
3902 /// ```rust
3903 /// // This lint is intentionally used to test the compiler's behavior
3904 /// // when an unstable lint is enabled without the corresponding feature gate.
3905 /// #![allow(test_unstable_lint)]
3906 /// ```
3907 ///
3908 /// {{produces}}
3909 ///
3910 /// ### Explanation
3911 ///
3912 /// In order to test the behavior of unstable lints, a permanently-unstable
3913 /// lint is required. This lint can be used to trigger warnings and errors
3914 /// from the compiler related to unstable lints.
3915 pub TEST_UNSTABLE_LINT,
3916 Deny,
3917 "this unstable lint is only for testing",
3918 @feature_gate = test_unstable_lint;
3919}
3920
3921declare_lint! {
3922 /// The `ffi_unwind_calls` lint detects calls to foreign functions or function pointers with
3923 /// `C-unwind` or other FFI-unwind ABIs.
3924 ///
3925 /// ### Example
3926 ///
3927 /// ```rust
3928 /// #![warn(ffi_unwind_calls)]
3929 ///
3930 /// unsafe extern "C-unwind" {
3931 /// fn foo();
3932 /// }
3933 ///
3934 /// fn bar() {
3935 /// unsafe { foo(); }
3936 /// let ptr: unsafe extern "C-unwind" fn() = foo;
3937 /// unsafe { ptr(); }
3938 /// }
3939 /// ```
3940 ///
3941 /// {{produces}}
3942 ///
3943 /// ### Explanation
3944 ///
3945 /// For crates containing such calls, if they are compiled with `-C panic=unwind` then the
3946 /// produced library cannot be linked with crates compiled with `-C panic=abort`. For crates
3947 /// that desire this ability it is therefore necessary to avoid such calls.
3948 pub FFI_UNWIND_CALLS,
3949 Allow,
3950 "call to foreign functions or function pointers with FFI-unwind ABI"
3951}
3952
3953declare_lint! {
3954 /// The `linker_messages` lint forwards warnings from the linker.
3955 ///
3956 /// ### Example
3957 ///
3958 /// ```rust,ignore (needs CLI args, platform-specific)
3959 /// #[warn(linker_messages)]
3960 /// extern "C" {
3961 /// fn foo();
3962 /// }
3963 /// fn main () { unsafe { foo(); } }
3964 /// ```
3965 ///
3966 /// On Linux, using `gcc -Wl,--warn-unresolved-symbols` as a linker, this will produce
3967 ///
3968 /// ```text
3969 /// warning: linker stderr: rust-lld: undefined symbol: foo
3970 /// >>> referenced by rust_out.69edbd30df4ae57d-cgu.0
3971 /// >>> rust_out.rust_out.69edbd30df4ae57d-cgu.0.rcgu.o:(rust_out::main::h3a90094b06757803)
3972 /// |
3973 /// note: the lint level is defined here
3974 /// --> warn.rs:1:9
3975 /// |
3976 /// 1 | #![warn(linker_messages)]
3977 /// | ^^^^^^^^^^^^^^^
3978 /// warning: 1 warning emitted
3979 /// ```
3980 ///
3981 /// ### Explanation
3982 ///
3983 /// Linkers emit platform-specific and program-specific warnings that cannot be predicted in
3984 /// advance by the Rust compiler. Such messages are ignored by default for now. While linker
3985 /// warnings could be very useful they have been ignored for many years by essentially all
3986 /// users, so we need to do a bit more work than just surfacing their text to produce a clear
3987 /// and actionable warning of similar quality to our other diagnostics. See this tracking
3988 /// issue for more details: <https://github.com/rust-lang/rust/issues/136096>.
3989 pub LINKER_MESSAGES,
3990 Allow,
3991 "warnings emitted at runtime by the target-specific linker program"
3992}
3993
3994declare_lint! {
3995 /// The `named_arguments_used_positionally` lint detects cases where named arguments are only
3996 /// used positionally in format strings. This usage is valid but potentially very confusing.
3997 ///
3998 /// ### Example
3999 ///
4000 /// ```rust,compile_fail
4001 /// #![deny(named_arguments_used_positionally)]
4002 /// fn main() {
4003 /// let _x = 5;
4004 /// println!("{}", _x = 1); // Prints 1, will trigger lint
4005 ///
4006 /// println!("{}", _x); // Prints 5, no lint emitted
4007 /// println!("{_x}", _x = _x); // Prints 5, no lint emitted
4008 /// }
4009 /// ```
4010 ///
4011 /// {{produces}}
4012 ///
4013 /// ### Explanation
4014 ///
4015 /// Rust formatting strings can refer to named arguments by their position, but this usage is
4016 /// potentially confusing. In particular, readers can incorrectly assume that the declaration
4017 /// of named arguments is an assignment (which would produce the unit type).
4018 /// For backwards compatibility, this is not a hard error.
4019 pub NAMED_ARGUMENTS_USED_POSITIONALLY,
4020 Warn,
4021 "named arguments in format used positionally"
4022}
4023
4024declare_lint! {
4025 /// The `never_type_fallback_flowing_into_unsafe` lint detects cases where never type fallback
4026 /// affects unsafe function calls.
4027 ///
4028 /// ### Never type fallback
4029 ///
4030 /// When the compiler sees a value of type [`!`] it implicitly inserts a coercion (if possible),
4031 /// to allow type check to infer any type:
4032 ///
4033 /// ```ignore (illustrative-and-has-placeholders)
4034 /// // this
4035 /// let x: u8 = panic!();
4036 ///
4037 /// // is (essentially) turned by the compiler into
4038 /// let x: u8 = absurd(panic!());
4039 ///
4040 /// // where absurd is a function with the following signature
4041 /// // (it's sound, because `!` always marks unreachable code):
4042 /// fn absurd<T>(never: !) -> T { ... }
4043 /// ```
4044 ///
4045 /// While it's convenient to be able to use non-diverging code in one of the branches (like
4046 /// `if a { b } else { return }`) this could lead to compilation errors:
4047 ///
4048 /// ```compile_fail
4049 /// // this
4050 /// { panic!() };
4051 ///
4052 /// // gets turned into this
4053 /// { absurd(panic!()) }; // error: can't infer the type of `absurd`
4054 /// ```
4055 ///
4056 /// To prevent such errors, compiler remembers where it inserted `absurd` calls, and if it
4057 /// can't infer their type, it sets the type to fallback. `{ absurd::<Fallback>(panic!()) };`.
4058 /// This is what is known as "never type fallback".
4059 ///
4060 /// ### Example
4061 ///
4062 /// ```rust,compile_fail
4063 /// #![deny(never_type_fallback_flowing_into_unsafe)]
4064 /// fn main() {
4065 /// if true {
4066 /// // return has type `!` which, is some cases, causes never type fallback
4067 /// return
4068 /// } else {
4069 /// // `zeroed` is an unsafe function, which returns an unbounded type
4070 /// unsafe { std::mem::zeroed() }
4071 /// };
4072 /// // depending on the fallback, `zeroed` may create `()` (which is completely sound),
4073 /// // or `!` (which is instant undefined behavior)
4074 /// }
4075 /// ```
4076 ///
4077 /// {{produces}}
4078 ///
4079 /// ### Explanation
4080 ///
4081 /// Due to historic reasons never type fallback was `()`, meaning that `!` got spontaneously
4082 /// coerced to `()`. There are plans to change that, but they may make the code such as above
4083 /// unsound. Instead of depending on the fallback, you should specify the type explicitly:
4084 /// ```
4085 /// if true {
4086 /// return
4087 /// } else {
4088 /// // type is explicitly specified, fallback can't hurt us no more
4089 /// unsafe { std::mem::zeroed::<()>() }
4090 /// };
4091 /// ```
4092 ///
4093 /// See [Tracking Issue for making `!` fall back to `!`](https://github.com/rust-lang/rust/issues/123748).
4094 ///
4095 /// [`!`]: https://doc.rust-lang.org/core/primitive.never.html
4096 /// [`()`]: https://doc.rust-lang.org/core/primitive.unit.html
4097 pub NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
4098 Warn,
4099 "never type fallback affecting unsafe function calls",
4100 @future_incompatible = FutureIncompatibleInfo {
4101 reason: FutureIncompatibilityReason::EditionAndFutureReleaseSemanticsChange(Edition::Edition2024),
4102 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/never-type-fallback.html>",
4103 report_in_deps: true,
4104 };
4105 @edition Edition2024 => Deny;
4106 report_in_external_macro
4107}
4108
4109declare_lint! {
4110 /// The `dependency_on_unit_never_type_fallback` lint detects cases where code compiles with
4111 /// [never type fallback] being [`()`], but will stop compiling with fallback being [`!`].
4112 ///
4113 /// [never type fallback]: https://doc.rust-lang.org/nightly/core/primitive.never.html#never-type-fallback
4114 /// [`!`]: https://doc.rust-lang.org/core/primitive.never.html
4115 /// [`()`]: https://doc.rust-lang.org/core/primitive.unit.html
4116 ///
4117 /// ### Example
4118 ///
4119 /// ```rust,compile_fail,edition2021
4120 /// #![deny(dependency_on_unit_never_type_fallback)]
4121 /// fn main() {
4122 /// if true {
4123 /// // return has type `!` which, is some cases, causes never type fallback
4124 /// return
4125 /// } else {
4126 /// // the type produced by this call is not specified explicitly,
4127 /// // so it will be inferred from the previous branch
4128 /// Default::default()
4129 /// };
4130 /// // depending on the fallback, this may compile (because `()` implements `Default`),
4131 /// // or it may not (because `!` does not implement `Default`)
4132 /// }
4133 /// ```
4134 ///
4135 /// {{produces}}
4136 ///
4137 /// ### Explanation
4138 ///
4139 /// Due to historic reasons never type fallback was `()`, meaning that `!` got spontaneously
4140 /// coerced to `()`. There are plans to change that, but they may make the code such as above
4141 /// not compile. Instead of depending on the fallback, you should specify the type explicitly:
4142 /// ```
4143 /// if true {
4144 /// return
4145 /// } else {
4146 /// // type is explicitly specified, fallback can't hurt us no more
4147 /// <() as Default>::default()
4148 /// };
4149 /// ```
4150 ///
4151 /// See [Tracking Issue for making `!` fall back to `!`](https://github.com/rust-lang/rust/issues/123748).
4152 pub DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK,
4153 Warn,
4154 "never type fallback affecting unsafe function calls",
4155 @future_incompatible = FutureIncompatibleInfo {
4156 reason: FutureIncompatibilityReason::EditionAndFutureReleaseError(Edition::Edition2024),
4157 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/never-type-fallback.html>",
4158 report_in_deps: true,
4159 };
4160 report_in_external_macro
4161}
4162
4163declare_lint! {
4164 /// The `invalid_macro_export_arguments` lint detects cases where `#[macro_export]` is being used with invalid arguments.
4165 ///
4166 /// ### Example
4167 ///
4168 /// ```rust,compile_fail
4169 /// #![deny(invalid_macro_export_arguments)]
4170 ///
4171 /// #[macro_export(invalid_parameter)]
4172 /// macro_rules! myMacro {
4173 /// () => {
4174 /// // [...]
4175 /// }
4176 /// }
4177 ///
4178 /// #[macro_export(too, many, items)]
4179 /// ```
4180 ///
4181 /// {{produces}}
4182 ///
4183 /// ### Explanation
4184 ///
4185 /// The only valid argument is `#[macro_export(local_inner_macros)]` or no argument (`#[macro_export]`).
4186 /// You can't have multiple arguments in a `#[macro_export(..)]`, or mention arguments other than `local_inner_macros`.
4187 ///
4188 pub INVALID_MACRO_EXPORT_ARGUMENTS,
4189 Warn,
4190 "\"invalid_parameter\" isn't a valid argument for `#[macro_export]`",
4191}
4192
4193declare_lint! {
4194 /// The `private_interfaces` lint detects types in a primary interface of an item,
4195 /// that are more private than the item itself. Primary interface of an item is all
4196 /// its interface except for bounds on generic parameters and where clauses.
4197 ///
4198 /// ### Example
4199 ///
4200 /// ```rust,compile_fail
4201 /// # #![allow(unused)]
4202 /// #![deny(private_interfaces)]
4203 /// struct SemiPriv;
4204 ///
4205 /// mod m1 {
4206 /// struct Priv;
4207 /// impl crate::SemiPriv {
4208 /// pub fn f(_: Priv) {}
4209 /// }
4210 /// }
4211 ///
4212 /// # fn main() {}
4213 /// ```
4214 ///
4215 /// {{produces}}
4216 ///
4217 /// ### Explanation
4218 ///
4219 /// Having something private in primary interface guarantees that
4220 /// the item will be unusable from outer modules due to type privacy.
4221 pub PRIVATE_INTERFACES,
4222 Warn,
4223 "private type in primary interface of an item",
4224}
4225
4226declare_lint! {
4227 /// The `private_bounds` lint detects types in a secondary interface of an item,
4228 /// that are more private than the item itself. Secondary interface of an item consists of
4229 /// bounds on generic parameters and where clauses, including supertraits for trait items.
4230 ///
4231 /// ### Example
4232 ///
4233 /// ```rust,compile_fail
4234 /// # #![allow(unused)]
4235 /// #![deny(private_bounds)]
4236 ///
4237 /// struct PrivTy;
4238 /// pub struct S
4239 /// where PrivTy:
4240 /// {}
4241 /// # fn main() {}
4242 /// ```
4243 ///
4244 /// {{produces}}
4245 ///
4246 /// ### Explanation
4247 ///
4248 /// Having private types or traits in item bounds makes it less clear what interface
4249 /// the item actually provides.
4250 pub PRIVATE_BOUNDS,
4251 Warn,
4252 "private type in secondary interface of an item",
4253}
4254
4255declare_lint! {
4256 /// The `unnameable_types` lint detects types for which you can get objects of that type,
4257 /// but cannot name the type itself.
4258 ///
4259 /// ### Example
4260 ///
4261 /// ```rust,compile_fail
4262 /// # #![allow(unused)]
4263 /// #![deny(unnameable_types)]
4264 /// mod m {
4265 /// pub struct S;
4266 /// }
4267 ///
4268 /// pub fn get_unnameable() -> m::S { m::S }
4269 /// # fn main() {}
4270 /// ```
4271 ///
4272 /// {{produces}}
4273 ///
4274 /// ### Explanation
4275 ///
4276 /// It is often expected that if you can obtain an object of type `T`, then
4277 /// you can name the type `T` as well; this lint attempts to enforce this rule.
4278 /// The recommended action is to either reexport the type properly to make it nameable,
4279 /// or document that users are not supposed to be able to name it for one reason or another.
4280 ///
4281 /// Besides types, this lint applies to traits because traits can also leak through signatures,
4282 /// and you may obtain objects of their `dyn Trait` or `impl Trait` types.
4283 pub UNNAMEABLE_TYPES,
4284 Allow,
4285 "effective visibility of a type is larger than the area in which it can be named",
4286}
4287
4288declare_lint! {
4289 /// The `unknown_or_malformed_diagnostic_attributes` lint detects unrecognized or otherwise malformed
4290 /// diagnostic attributes.
4291 ///
4292 /// ### Example
4293 ///
4294 /// ```rust
4295 /// #![feature(diagnostic_namespace)]
4296 /// #[diagnostic::does_not_exist]
4297 /// struct Foo;
4298 /// ```
4299 ///
4300 /// {{produces}}
4301 ///
4302 ///
4303 /// ### Explanation
4304 ///
4305 /// It is usually a mistake to specify a diagnostic attribute that does not exist. Check
4306 /// the spelling, and check the diagnostic attribute listing for the correct name. Also
4307 /// consider if you are using an old version of the compiler, and the attribute
4308 /// is only available in a newer version.
4309 pub UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
4310 Warn,
4311 "unrecognized or malformed diagnostic attribute",
4312}
4313
4314declare_lint! {
4315 /// The `ambiguous_glob_imports` lint detects glob imports that should report ambiguity
4316 /// errors, but previously didn't do that due to rustc bugs.
4317 ///
4318 /// ### Example
4319 ///
4320 /// ```rust,compile_fail
4321 /// #![deny(ambiguous_glob_imports)]
4322 /// pub fn foo() -> u32 {
4323 /// use sub::*;
4324 /// C
4325 /// }
4326 ///
4327 /// mod sub {
4328 /// mod mod1 { pub const C: u32 = 1; }
4329 /// mod mod2 { pub const C: u32 = 2; }
4330 ///
4331 /// pub use mod1::*;
4332 /// pub use mod2::*;
4333 /// }
4334 /// ```
4335 ///
4336 /// {{produces}}
4337 ///
4338 /// ### Explanation
4339 ///
4340 /// Previous versions of Rust compile it successfully because it
4341 /// had lost the ambiguity error when resolve `use sub::mod2::*`.
4342 ///
4343 /// This is a [future-incompatible] lint to transition this to a
4344 /// hard error in the future.
4345 ///
4346 /// [future-incompatible]: ../index.md#future-incompatible-lints
4347 pub AMBIGUOUS_GLOB_IMPORTS,
4348 Warn,
4349 "detects certain glob imports that require reporting an ambiguity error",
4350 @future_incompatible = FutureIncompatibleInfo {
4351 reason: FutureIncompatibilityReason::FutureReleaseError,
4352 reference: "issue #114095 <https://github.com/rust-lang/rust/issues/114095>",
4353 };
4354}
4355
4356declare_lint! {
4357 /// The `refining_impl_trait_reachable` lint detects `impl Trait` return
4358 /// types in method signatures that are refined by a publically reachable
4359 /// trait implementation, meaning the implementation adds information about
4360 /// the return type that is not present in the trait.
4361 ///
4362 /// ### Example
4363 ///
4364 /// ```rust,compile_fail
4365 /// #![deny(refining_impl_trait)]
4366 ///
4367 /// use std::fmt::Display;
4368 ///
4369 /// pub trait AsDisplay {
4370 /// fn as_display(&self) -> impl Display;
4371 /// }
4372 ///
4373 /// impl<'s> AsDisplay for &'s str {
4374 /// fn as_display(&self) -> Self {
4375 /// *self
4376 /// }
4377 /// }
4378 ///
4379 /// fn main() {
4380 /// // users can observe that the return type of
4381 /// // `<&str as AsDisplay>::as_display()` is `&str`.
4382 /// let _x: &str = "".as_display();
4383 /// }
4384 /// ```
4385 ///
4386 /// {{produces}}
4387 ///
4388 /// ### Explanation
4389 ///
4390 /// Callers of methods for types where the implementation is known are
4391 /// able to observe the types written in the impl signature. This may be
4392 /// intended behavior, but may also lead to implementation details being
4393 /// revealed unintentionally. In particular, it may pose a semver hazard
4394 /// for authors of libraries who do not wish to make stronger guarantees
4395 /// about the types than what is written in the trait signature.
4396 ///
4397 /// `refining_impl_trait` is a lint group composed of two lints:
4398 ///
4399 /// * `refining_impl_trait_reachable`, for refinements that are publically
4400 /// reachable outside a crate, and
4401 /// * `refining_impl_trait_internal`, for refinements that are only visible
4402 /// within a crate.
4403 ///
4404 /// We are seeking feedback on each of these lints; see issue
4405 /// [#121718](https://github.com/rust-lang/rust/issues/121718) for more
4406 /// information.
4407 pub REFINING_IMPL_TRAIT_REACHABLE,
4408 Warn,
4409 "impl trait in impl method signature does not match trait method signature",
4410}
4411
4412declare_lint! {
4413 /// The `refining_impl_trait_internal` lint detects `impl Trait` return
4414 /// types in method signatures that are refined by a trait implementation,
4415 /// meaning the implementation adds information about the return type that
4416 /// is not present in the trait.
4417 ///
4418 /// ### Example
4419 ///
4420 /// ```rust,compile_fail
4421 /// #![deny(refining_impl_trait)]
4422 ///
4423 /// use std::fmt::Display;
4424 ///
4425 /// trait AsDisplay {
4426 /// fn as_display(&self) -> impl Display;
4427 /// }
4428 ///
4429 /// impl<'s> AsDisplay for &'s str {
4430 /// fn as_display(&self) -> Self {
4431 /// *self
4432 /// }
4433 /// }
4434 ///
4435 /// fn main() {
4436 /// // users can observe that the return type of
4437 /// // `<&str as AsDisplay>::as_display()` is `&str`.
4438 /// let _x: &str = "".as_display();
4439 /// }
4440 /// ```
4441 ///
4442 /// {{produces}}
4443 ///
4444 /// ### Explanation
4445 ///
4446 /// Callers of methods for types where the implementation is known are
4447 /// able to observe the types written in the impl signature. This may be
4448 /// intended behavior, but may also lead to implementation details being
4449 /// revealed unintentionally. In particular, it may pose a semver hazard
4450 /// for authors of libraries who do not wish to make stronger guarantees
4451 /// about the types than what is written in the trait signature.
4452 ///
4453 /// `refining_impl_trait` is a lint group composed of two lints:
4454 ///
4455 /// * `refining_impl_trait_reachable`, for refinements that are publically
4456 /// reachable outside a crate, and
4457 /// * `refining_impl_trait_internal`, for refinements that are only visible
4458 /// within a crate.
4459 ///
4460 /// We are seeking feedback on each of these lints; see issue
4461 /// [#121718](https://github.com/rust-lang/rust/issues/121718) for more
4462 /// information.
4463 pub REFINING_IMPL_TRAIT_INTERNAL,
4464 Warn,
4465 "impl trait in impl method signature does not match trait method signature",
4466}
4467
4468declare_lint! {
4469 /// The `elided_lifetimes_in_associated_constant` lint detects elided lifetimes
4470 /// in associated constants when there are other lifetimes in scope. This was
4471 /// accidentally supported, and this lint was later relaxed to allow eliding
4472 /// lifetimes to `'static` when there are no lifetimes in scope.
4473 ///
4474 /// ### Example
4475 ///
4476 /// ```rust,compile_fail
4477 /// #![deny(elided_lifetimes_in_associated_constant)]
4478 ///
4479 /// struct Foo<'a>(&'a ());
4480 ///
4481 /// impl<'a> Foo<'a> {
4482 /// const STR: &str = "hello, world";
4483 /// }
4484 /// ```
4485 ///
4486 /// {{produces}}
4487 ///
4488 /// ### Explanation
4489 ///
4490 /// Previous version of Rust
4491 ///
4492 /// Implicit static-in-const behavior was decided [against] for associated
4493 /// constants because of ambiguity. This, however, regressed and the compiler
4494 /// erroneously treats elided lifetimes in associated constants as lifetime
4495 /// parameters on the impl.
4496 ///
4497 /// This is a [future-incompatible] lint to transition this to a
4498 /// hard error in the future.
4499 ///
4500 /// [against]: https://github.com/rust-lang/rust/issues/38831
4501 /// [future-incompatible]: ../index.md#future-incompatible-lints
4502 pub ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
4503 Deny,
4504 "elided lifetimes cannot be used in associated constants in impls",
4505 @future_incompatible = FutureIncompatibleInfo {
4506 reason: FutureIncompatibilityReason::FutureReleaseError,
4507 reference: "issue #115010 <https://github.com/rust-lang/rust/issues/115010>",
4508 };
4509}
4510
4511declare_lint! {
4512 /// The `private_macro_use` lint detects private macros that are imported
4513 /// with `#[macro_use]`.
4514 ///
4515 /// ### Example
4516 ///
4517 /// ```rust,ignore (needs extern crate)
4518 /// // extern_macro.rs
4519 /// macro_rules! foo_ { () => {}; }
4520 /// use foo_ as foo;
4521 ///
4522 /// // code.rs
4523 ///
4524 /// #![deny(private_macro_use)]
4525 ///
4526 /// #[macro_use]
4527 /// extern crate extern_macro;
4528 ///
4529 /// fn main() {
4530 /// foo!();
4531 /// }
4532 /// ```
4533 ///
4534 /// This will produce:
4535 ///
4536 /// ```text
4537 /// error: cannot find macro `foo` in this scope
4538 /// ```
4539 ///
4540 /// ### Explanation
4541 ///
4542 /// This lint arises from overlooking visibility checks for macros
4543 /// in an external crate.
4544 ///
4545 /// This is a [future-incompatible] lint to transition this to a
4546 /// hard error in the future.
4547 ///
4548 /// [future-incompatible]: ../index.md#future-incompatible-lints
4549 pub PRIVATE_MACRO_USE,
4550 Warn,
4551 "detects certain macro bindings that should not be re-exported",
4552 @future_incompatible = FutureIncompatibleInfo {
4553 reason: FutureIncompatibilityReason::FutureReleaseError,
4554 reference: "issue #120192 <https://github.com/rust-lang/rust/issues/120192>",
4555 };
4556}
4557
4558declare_lint! {
4559 /// The `uncovered_param_in_projection` lint detects a violation of one of Rust's orphan rules for
4560 /// foreign trait implementations that concerns the use of type parameters inside trait associated
4561 /// type paths ("projections") whose output may not be a local type that is mistakenly considered
4562 /// to "cover" said parameters which is **unsound** and which may be rejected by a future version
4563 /// of the compiler.
4564 ///
4565 /// Originally reported in [#99554].
4566 ///
4567 /// [#99554]: https://github.com/rust-lang/rust/issues/99554
4568 ///
4569 /// ### Example
4570 ///
4571 /// ```rust,ignore (dependent)
4572 /// // dependency.rs
4573 /// #![crate_type = "lib"]
4574 ///
4575 /// pub trait Trait<T, U> {}
4576 /// ```
4577 ///
4578 /// ```edition2021,ignore (needs dependency)
4579 /// // dependent.rs
4580 /// trait Identity {
4581 /// type Output;
4582 /// }
4583 ///
4584 /// impl<T> Identity for T {
4585 /// type Output = T;
4586 /// }
4587 ///
4588 /// struct Local;
4589 ///
4590 /// impl<T> dependency::Trait<Local, T> for <T as Identity>::Output {}
4591 ///
4592 /// fn main() {}
4593 /// ```
4594 ///
4595 /// This will produce:
4596 ///
4597 /// ```text
4598 /// warning[E0210]: type parameter `T` must be covered by another type when it appears before the first local type (`Local`)
4599 /// --> dependent.rs:11:6
4600 /// |
4601 /// 11 | impl<T> dependency::Trait<Local, T> for <T as Identity>::Output {}
4602 /// | ^ type parameter `T` must be covered by another type when it appears before the first local type (`Local`)
4603 /// |
4604 /// = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4605 /// = note: for more information, see issue #124559 <https://github.com/rust-lang/rust/issues/124559>
4606 /// = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local, and no uncovered type parameters appear before that first local type
4607 /// = note: in this case, 'before' refers to the following order: `impl<..> ForeignTrait<T1, ..., Tn> for T0`, where `T0` is the first and `Tn` is the last
4608 /// = note: `#[warn(uncovered_param_in_projection)]` on by default
4609 /// ```
4610 ///
4611 /// ### Explanation
4612 ///
4613 /// FIXME(fmease): Write explainer.
4614 pub UNCOVERED_PARAM_IN_PROJECTION,
4615 Warn,
4616 "impl contains type parameters that are not covered",
4617 @future_incompatible = FutureIncompatibleInfo {
4618 reason: FutureIncompatibilityReason::FutureReleaseError,
4619 reference: "issue #124559 <https://github.com/rust-lang/rust/issues/124559>",
4620 };
4621}
4622
4623declare_lint! {
4624 /// The `deprecated_safe_2024` lint detects unsafe functions being used as
4625 /// safe functions.
4626 ///
4627 /// ### Example
4628 ///
4629 /// ```rust,edition2021,compile_fail
4630 /// #![deny(deprecated_safe)]
4631 /// // edition 2021
4632 /// use std::env;
4633 /// fn enable_backtrace() {
4634 /// env::set_var("RUST_BACKTRACE", "1");
4635 /// }
4636 /// ```
4637 ///
4638 /// {{produces}}
4639 ///
4640 /// ### Explanation
4641 ///
4642 /// Rust [editions] allow the language to evolve without breaking backward
4643 /// compatibility. This lint catches code that uses `unsafe` functions that
4644 /// were declared as safe (non-`unsafe`) in editions prior to Rust 2024. If
4645 /// you switch the compiler to Rust 2024 without updating the code, then it
4646 /// will fail to compile if you are using a function previously marked as
4647 /// safe.
4648 ///
4649 /// You can audit the code to see if it suffices the preconditions of the
4650 /// `unsafe` code, and if it does, you can wrap it in an `unsafe` block. If
4651 /// you can't fulfill the preconditions, you probably need to switch to a
4652 /// different way of doing what you want to achieve.
4653 ///
4654 /// This lint can automatically wrap the calls in `unsafe` blocks, but this
4655 /// obviously cannot verify that the preconditions of the `unsafe`
4656 /// functions are fulfilled, so that is still up to the user.
4657 ///
4658 /// The lint is currently "allow" by default, but that might change in the
4659 /// future.
4660 ///
4661 /// [editions]: https://doc.rust-lang.org/edition-guide/
4662 pub DEPRECATED_SAFE_2024,
4663 Allow,
4664 "detects unsafe functions being used as safe functions",
4665 @future_incompatible = FutureIncompatibleInfo {
4666 reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024),
4667 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/newly-unsafe-functions.html>",
4668 };
4669}
4670
4671declare_lint! {
4672 /// The `missing_unsafe_on_extern` lint detects missing unsafe keyword on extern declarations.
4673 ///
4674 /// ### Example
4675 ///
4676 /// ```rust,edition2021
4677 /// #![warn(missing_unsafe_on_extern)]
4678 /// #![allow(dead_code)]
4679 ///
4680 /// extern "C" {
4681 /// fn foo(_: i32);
4682 /// }
4683 ///
4684 /// fn main() {}
4685 /// ```
4686 ///
4687 /// {{produces}}
4688 ///
4689 /// ### Explanation
4690 ///
4691 /// Declaring extern items, even without ever using them, can cause Undefined Behavior. We
4692 /// should consider all sources of Undefined Behavior to be unsafe.
4693 ///
4694 /// This is a [future-incompatible] lint to transition this to a
4695 /// hard error in the future.
4696 ///
4697 /// [future-incompatible]: ../index.md#future-incompatible-lints
4698 pub MISSING_UNSAFE_ON_EXTERN,
4699 Allow,
4700 "detects missing unsafe keyword on extern declarations",
4701 @future_incompatible = FutureIncompatibleInfo {
4702 reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024),
4703 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-extern.html>",
4704 };
4705}
4706
4707declare_lint! {
4708 /// The `unsafe_attr_outside_unsafe` lint detects a missing unsafe keyword
4709 /// on attributes considered unsafe.
4710 ///
4711 /// ### Example
4712 ///
4713 /// ```rust,edition2021
4714 /// #![warn(unsafe_attr_outside_unsafe)]
4715 ///
4716 /// #[no_mangle]
4717 /// extern "C" fn foo() {}
4718 ///
4719 /// fn main() {}
4720 /// ```
4721 ///
4722 /// {{produces}}
4723 ///
4724 /// ### Explanation
4725 ///
4726 /// Some attributes (e.g. `no_mangle`, `export_name`, `link_section` -- see
4727 /// [issue #82499] for a more complete list) are considered "unsafe" attributes.
4728 /// An unsafe attribute must only be used inside unsafe(...).
4729 ///
4730 /// This lint can automatically wrap the attributes in `unsafe(...)` , but this
4731 /// obviously cannot verify that the preconditions of the `unsafe`
4732 /// attributes are fulfilled, so that is still up to the user.
4733 ///
4734 /// The lint is currently "allow" by default, but that might change in the
4735 /// future.
4736 ///
4737 /// [editions]: https://doc.rust-lang.org/edition-guide/
4738 /// [issue #82499]: https://github.com/rust-lang/rust/issues/82499
4739 pub UNSAFE_ATTR_OUTSIDE_UNSAFE,
4740 Allow,
4741 "detects unsafe attributes outside of unsafe",
4742 @future_incompatible = FutureIncompatibleInfo {
4743 reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024),
4744 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-attributes.html>",
4745 };
4746}
4747
4748declare_lint! {
4749 /// The `out_of_scope_macro_calls` lint detects `macro_rules` called when they are not in scope,
4750 /// above their definition, which may happen in key-value attributes.
4751 ///
4752 /// ### Example
4753 ///
4754 /// ```rust
4755 /// #![doc = in_root!()]
4756 ///
4757 /// macro_rules! in_root { () => { "" } }
4758 ///
4759 /// fn main() {}
4760 /// ```
4761 ///
4762 /// {{produces}}
4763 ///
4764 /// ### Explanation
4765 ///
4766 /// The scope in which a `macro_rules` item is visible starts at that item and continues
4767 /// below it. This is more similar to `let` than to other items, which are in scope both above
4768 /// and below their definition.
4769 /// Due to a bug `macro_rules` were accidentally in scope inside some key-value attributes
4770 /// above their definition. The lint catches such cases.
4771 /// To address the issue turn the `macro_rules` into a regularly scoped item by importing it
4772 /// with `use`.
4773 ///
4774 /// This is a [future-incompatible] lint to transition this to a
4775 /// hard error in the future.
4776 ///
4777 /// [future-incompatible]: ../index.md#future-incompatible-lints
4778 pub OUT_OF_SCOPE_MACRO_CALLS,
4779 Warn,
4780 "detects out of scope calls to `macro_rules` in key-value attributes",
4781 @future_incompatible = FutureIncompatibleInfo {
4782 reason: FutureIncompatibilityReason::FutureReleaseError,
4783 reference: "issue #124535 <https://github.com/rust-lang/rust/issues/124535>",
4784 };
4785}
4786
4787declare_lint! {
4788 /// The `supertrait_item_shadowing_usage` lint detects when the
4789 /// usage of an item that is provided by both a subtrait and supertrait
4790 /// is shadowed, preferring the subtrait.
4791 ///
4792 /// ### Example
4793 ///
4794 /// ```rust,compile_fail
4795 /// #![feature(supertrait_item_shadowing)]
4796 /// #![deny(supertrait_item_shadowing_usage)]
4797 ///
4798 /// trait Upstream {
4799 /// fn hello(&self) {}
4800 /// }
4801 /// impl<T> Upstream for T {}
4802 ///
4803 /// trait Downstream: Upstream {
4804 /// fn hello(&self) {}
4805 /// }
4806 /// impl<T> Downstream for T {}
4807 ///
4808 /// struct MyType;
4809 /// MyType.hello();
4810 /// ```
4811 ///
4812 /// {{produces}}
4813 ///
4814 /// ### Explanation
4815 ///
4816 /// RFC 3624 specified a heuristic in which a supertrait item would be
4817 /// shadowed by a subtrait item when ambiguity occurs during item
4818 /// selection. In order to mitigate side-effects of this happening
4819 /// silently, this lint detects these cases when users want to deny them
4820 /// or fix the call sites.
4821 pub SUPERTRAIT_ITEM_SHADOWING_USAGE,
4822 // FIXME(supertrait_item_shadowing): It is not decided if this should
4823 // warn by default at the call site.
4824 Allow,
4825 "detects when a supertrait item is shadowed by a subtrait item",
4826 @feature_gate = supertrait_item_shadowing;
4827}
4828
4829declare_lint! {
4830 /// The `supertrait_item_shadowing_definition` lint detects when the
4831 /// definition of an item that is provided by both a subtrait and
4832 /// supertrait is shadowed, preferring the subtrait.
4833 ///
4834 /// ### Example
4835 ///
4836 /// ```rust,compile_fail
4837 /// #![feature(supertrait_item_shadowing)]
4838 /// #![deny(supertrait_item_shadowing_definition)]
4839 ///
4840 /// trait Upstream {
4841 /// fn hello(&self) {}
4842 /// }
4843 /// impl<T> Upstream for T {}
4844 ///
4845 /// trait Downstream: Upstream {
4846 /// fn hello(&self) {}
4847 /// }
4848 /// impl<T> Downstream for T {}
4849 /// ```
4850 ///
4851 /// {{produces}}
4852 ///
4853 /// ### Explanation
4854 ///
4855 /// RFC 3624 specified a heuristic in which a supertrait item would be
4856 /// shadowed by a subtrait item when ambiguity occurs during item
4857 /// selection. In order to mitigate side-effects of this happening
4858 /// silently, this lint detects these cases when users want to deny them
4859 /// or fix their trait definitions.
4860 pub SUPERTRAIT_ITEM_SHADOWING_DEFINITION,
4861 // FIXME(supertrait_item_shadowing): It is not decided if this should
4862 // warn by default at the usage site.
4863 Allow,
4864 "detects when a supertrait item is shadowed by a subtrait item",
4865 @feature_gate = supertrait_item_shadowing;
4866}
4867
4868declare_lint! {
4869 /// The `tail_expr_drop_order` lint looks for those values generated at the tail expression location,
4870 /// that runs a custom `Drop` destructor.
4871 /// Some of them may be dropped earlier in Edition 2024 that they used to in Edition 2021 and prior.
4872 /// This lint detects those cases and provides you information on those values and their custom destructor implementations.
4873 /// Your discretion on this information is required.
4874 ///
4875 /// ### Example
4876 /// ```rust,edition2021
4877 /// #![warn(tail_expr_drop_order)]
4878 /// struct Droppy(i32);
4879 /// impl Droppy {
4880 /// fn get(&self) -> i32 {
4881 /// self.0
4882 /// }
4883 /// }
4884 /// impl Drop for Droppy {
4885 /// fn drop(&mut self) {
4886 /// // This is a custom destructor and it induces side-effects that is observable
4887 /// // especially when the drop order at a tail expression changes.
4888 /// println!("loud drop {}", self.0);
4889 /// }
4890 /// }
4891 /// fn edition_2021() -> i32 {
4892 /// let another_droppy = Droppy(0);
4893 /// Droppy(1).get()
4894 /// }
4895 /// fn main() {
4896 /// edition_2021();
4897 /// }
4898 /// ```
4899 ///
4900 /// {{produces}}
4901 ///
4902 /// ### Explanation
4903 ///
4904 /// In tail expression of blocks or function bodies,
4905 /// values of type with significant `Drop` implementation has an ill-specified drop order
4906 /// before Edition 2024 so that they are dropped only after dropping local variables.
4907 /// Edition 2024 introduces a new rule with drop orders for them,
4908 /// so that they are dropped first before dropping local variables.
4909 ///
4910 /// A significant `Drop::drop` destructor here refers to an explicit, arbitrary
4911 /// implementation of the `Drop` trait on the type, with exceptions including `Vec`,
4912 /// `Box`, `Rc`, `BTreeMap` and `HashMap` that are marked by the compiler otherwise
4913 /// so long that the generic types have no significant destructor recursively.
4914 /// In other words, a type has a significant drop destructor when it has a `Drop` implementation
4915 /// or its destructor invokes a significant destructor on a type.
4916 /// Since we cannot completely reason about the change by just inspecting the existence of
4917 /// a significant destructor, this lint remains only a suggestion and is set to `allow` by default.
4918 ///
4919 /// This lint only points out the issue with `Droppy`, which will be dropped before `another_droppy`
4920 /// does in Edition 2024.
4921 /// No fix will be proposed by this lint.
4922 /// However, the most probable fix is to hoist `Droppy` into its own local variable binding.
4923 /// ```rust
4924 /// struct Droppy(i32);
4925 /// impl Droppy {
4926 /// fn get(&self) -> i32 {
4927 /// self.0
4928 /// }
4929 /// }
4930 /// fn edition_2024() -> i32 {
4931 /// let value = Droppy(0);
4932 /// let another_droppy = Droppy(1);
4933 /// value.get()
4934 /// }
4935 /// ```
4936 pub TAIL_EXPR_DROP_ORDER,
4937 Allow,
4938 "Detect and warn on significant change in drop order in tail expression location",
4939 @future_incompatible = FutureIncompatibleInfo {
4940 reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024),
4941 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/temporary-tail-expr-scope.html>",
4942 };
4943}
4944
4945declare_lint! {
4946 /// The `rust_2024_guarded_string_incompatible_syntax` lint detects `#` tokens
4947 /// that will be parsed as part of a guarded string literal in Rust 2024.
4948 ///
4949 /// ### Example
4950 ///
4951 /// ```rust,edition2021,compile_fail
4952 /// #![deny(rust_2024_guarded_string_incompatible_syntax)]
4953 ///
4954 /// macro_rules! m {
4955 /// (# $x:expr #) => ();
4956 /// (# $x:expr) => ();
4957 /// }
4958 ///
4959 /// m!(#"hey"#);
4960 /// m!(#"hello");
4961 /// ```
4962 ///
4963 /// {{produces}}
4964 ///
4965 /// ### Explanation
4966 ///
4967 /// Prior to Rust 2024, `#"hey"#` is three tokens: the first `#`
4968 /// followed by the string literal `"hey"` then the final `#`.
4969 /// In Rust 2024, the whole sequence is considered a single token.
4970 ///
4971 /// This lint suggests to add whitespace between the leading `#`
4972 /// and the string to keep them separated in Rust 2024.
4973 // Allow this lint -- rustdoc doesn't yet support threading edition into this lint's parser.
4974 #[allow(rustdoc::invalid_rust_codeblocks)]
4975 pub RUST_2024_GUARDED_STRING_INCOMPATIBLE_SYNTAX,
4976 Allow,
4977 "will be parsed as a guarded string in Rust 2024",
4978 @future_incompatible = FutureIncompatibleInfo {
4979 reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024),
4980 reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/reserved-syntax.html>",
4981 };
4982 crate_level_only
4983}
4984
4985declare_lint! {
4986 /// The `aarch64_softfloat_neon` lint detects usage of `#[target_feature(enable = "neon")]` on
4987 /// softfloat aarch64 targets. Enabling this target feature causes LLVM to alter the ABI of
4988 /// function calls, making this attribute unsound to use.
4989 ///
4990 /// ### Example
4991 ///
4992 /// ```rust,ignore (needs aarch64-unknown-none-softfloat)
4993 /// #[target_feature(enable = "neon")]
4994 /// fn with_neon() {}
4995 /// ```
4996 ///
4997 /// This will produce:
4998 ///
4999 /// ```text
5000 /// error: enabling the `neon` target feature on the current target is unsound due to ABI issues
5001 /// --> $DIR/abi-incompatible-target-feature-attribute-fcw.rs:11:18
5002 /// |
5003 /// | #[target_feature(enable = "neon")]
5004 /// | ^^^^^^^^^^^^^^^
5005 /// |
5006 /// = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
5007 /// = note: for more information, see issue #134375 <https://github.com/rust-lang/rust/issues/134375>
5008 /// ```
5009 ///
5010 /// ### Explanation
5011 ///
5012 /// If a function like `with_neon` above ends up containing calls to LLVM builtins, those will
5013 /// not use the correct ABI. This is caused by a lack of support in LLVM for mixing code with
5014 /// and without the `neon` target feature. The target feature should never have been stabilized
5015 /// on this target due to this issue, but the problem was not known at the time of
5016 /// stabilization.
5017 pub AARCH64_SOFTFLOAT_NEON,
5018 Warn,
5019 "detects code that could be affected by ABI issues on aarch64 softfloat targets",
5020 @future_incompatible = FutureIncompatibleInfo {
5021 reason: FutureIncompatibilityReason::FutureReleaseError,
5022 reference: "issue #134375 <https://github.com/rust-lang/rust/issues/134375>",
5023 report_in_deps: true,
5024 };
5025}