@@ -493,10 +493,11 @@ much faster to work on.
493
493
[ `rustc_lint_defs` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/index.html
494
494
495
495
Every lint is implemented via a ` struct ` that implements the ` LintPass ` ` trait `
496
- (you also implement one of the more specific lint pass traits, either
497
- ` EarlyLintPass ` or ` LateLintPass ` ). The trait implementation allows you to
498
- check certain syntactic constructs as the linter walks the source code. You can
499
- then choose to emit lints in a very similar way to compile errors.
496
+ (you can also implement one of the more specific lint pass traits, either
497
+ ` EarlyLintPass ` or ` LateLintPass ` depending on when is best for your lint to run).
498
+ The trait implementation allows you to check certain syntactic constructs
499
+ as the linter walks the AST. You can then choose to emit lints in a
500
+ very similar way to compile errors.
500
501
501
502
You also declare the metadata of a particular lint via the ` declare_lint! `
502
503
macro. This includes the name, the default level, a short description, and some
@@ -566,7 +567,7 @@ impl EarlyLintPass for WhileTrue {
566
567
}
567
568
```
568
569
569
- ### Edition-gated Lints
570
+ ### Edition-gated lints
570
571
571
572
Sometimes we want to change the behavior of a lint in a new edition. To do this,
572
573
we just add the transition to our invocation of ` declare_lint! ` :
@@ -583,6 +584,18 @@ declare_lint! {
583
584
This makes the ` ANONYMOUS_PARAMETERS ` lint allow-by-default in the 2015 edition
584
585
but warn-by-default in the 2018 edition.
585
586
587
+ ### Future-incompatible lints
588
+
589
+ Future-incompatible lints are for signalling to the user that code they have
590
+ written may not compile in the future. In general, future-incompatible code
591
+ exists for two reasons:
592
+ * the user has written unsound code that the compiler mistakenly accepted. While
593
+ it is within Rust's backwards compatibility guarantees to fix the soundness hole
594
+ (breaking the user's code), the lint is there to warn the user that this will happen
595
+ in some upcoming version * regardless of which edition they are on* .
596
+ * the user has written code that will either no longer compiler * or* will change
597
+ meaning in an upcoming edition.
598
+
586
599
A future-incompatible lint should be declared with the ` @future_incompatible `
587
600
additional "field":
588
601
@@ -593,39 +606,34 @@ declare_lint! {
593
606
"detects anonymous parameters",
594
607
@future_incompatible = FutureIncompatibleInfo {
595
608
reference: "issue #41686 <https://github.com/rust-lang/rust/issues/41686>",
596
- edition: Some (Edition::Edition2018),
609
+ reason: FutureIncompatibilityReason::EditionError (Edition::Edition2018),
597
610
};
598
611
}
599
612
```
600
613
601
- If you need a combination of options that's not supported by the ` declare_lint! `
602
- macro, you can always define your own static with a type of ` &Lint ` but this is
603
- (as of <!-- date: 2021-01 --> January 2021) linted against in the compiler tree .
614
+ Notice the ` reason ` field which describes why the future-incompatible change is happening.
615
+ This will change the diagnostic message the user receives as well as determine which
616
+ lint groups the lint is added to .
604
617
605
- <a id =" future-incompatible " ></a >
606
- #### Guidelines for creating a future incompatibility lint
607
-
608
- - Create a lint defaulting to warn as normal, with ideally the same error
609
- message you would normally give.
610
- - Add a suitable reference, typically an RFC or tracking issue. Go ahead
611
- and include the full URL, sort items in ascending order of issue numbers.
612
- - Later, change lint to error.
613
- - Eventually, remove lint.
618
+ If you need a combination of options that's not supported by the
619
+ ` declare_lint! ` macro, you can always change the ` declare_lint! ` macro
620
+ to support this.
614
621
615
622
### Renaming or removing a lint
616
623
617
- A lint can be renamed or removed, which will trigger a warning if a user tries
624
+ If it is determined that a lint is either improperly named or no longer needed,
625
+ the lint must be registered for renaming or removal, which will trigger a warning if a user tries
618
626
to use the old lint name. To declare a rename/remove, add a line with
619
627
[ ` store.register_renamed ` ] or [ ` store.register_removed ` ] to the code of the
620
- [ ` register_builtins ` ] function.
628
+ [ ` rustc_lint:: register_builtins` ] function.
621
629
622
630
``` rust,ignore
623
631
store.register_renamed("single_use_lifetime", "single_use_lifetimes");
624
632
```
625
633
626
634
[ `store.register_renamed` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/struct.LintStore.html#method.register_renamed
627
635
[ `store.register_removed` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/struct.LintStore.html#method.register_removed
628
- [ `register_builtins` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/fn.register_builtins.html
636
+ [ `rustc_lint:: register_builtins` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/fn.register_builtins.html
629
637
630
638
### Lint Groups
631
639
@@ -649,6 +657,14 @@ This defines the `nonstandard_style` group which turns on the listed lints. A
649
657
user can turn on these lints with a ` !#[warn(nonstandard_style)] ` attribute in
650
658
the source code, or by passing ` -W nonstandard-style ` on the command line.
651
659
660
+ Some lint groups are created automatically in ` LintStore::register_lints ` . For instance,
661
+ any lint declared with ` FutureIncompatibleInfo ` where the reason is
662
+ ` FutureIncompatibilityReason::FutureReleaseError ` (the default when
663
+ ` @future_incompatible ` is used in ` declare_lint! ` ), will be added to
664
+ the ` future_incompatible ` lint group. Editions also have their own lint groups
665
+ (e.g., ` rust_2021_compatibility ` ) automatically generated for any lints signaling
666
+ future-incompatible code that will break in the specified edition.
667
+
652
668
### Linting early in the compiler
653
669
654
670
On occasion, you may need to define a lint that runs before the linting system
@@ -682,7 +698,7 @@ then dumped into the `Session::buffered_lints` used by the rest of the compiler.
682
698
683
699
The compiler accepts an ` --error-format json ` flag to output
684
700
diagnostics as JSON objects (for the benefit of tools such as `cargo
685
- fix` or the RLS). It looks like this—
701
+ fix` or the RLS). It looks like this:
686
702
687
703
``` console
688
704
$ rustc json_error_demo.rs --error-format json
0 commit comments