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