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