Skip to content

Commit 9636fa9

Browse files
rylevjyn514
authored andcommitted
Update information on lints particularly on future-incompatible
1 parent a4e29c5 commit 9636fa9

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

src/diagnostics.md

+38-22
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,11 @@ much faster to work on.
493493
[`rustc_lint_defs`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/index.html
494494

495495
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.
500501

501502
You also declare the metadata of a particular lint via the `declare_lint!`
502503
macro. This includes the name, the default level, a short description, and some
@@ -566,7 +567,7 @@ impl EarlyLintPass for WhileTrue {
566567
}
567568
```
568569

569-
### Edition-gated Lints
570+
### Edition-gated lints
570571

571572
Sometimes we want to change the behavior of a lint in a new edition. To do this,
572573
we just add the transition to our invocation of `declare_lint!`:
@@ -583,6 +584,18 @@ declare_lint! {
583584
This makes the `ANONYMOUS_PARAMETERS` lint allow-by-default in the 2015 edition
584585
but warn-by-default in the 2018 edition.
585586

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+
586599
A future-incompatible lint should be declared with the `@future_incompatible`
587600
additional "field":
588601

@@ -593,39 +606,34 @@ declare_lint! {
593606
"detects anonymous parameters",
594607
@future_incompatible = FutureIncompatibleInfo {
595608
reference: "issue #41686 <https://github.com/rust-lang/rust/issues/41686>",
596-
edition: Some(Edition::Edition2018),
609+
reason: FutureIncompatibilityReason::EditionError(Edition::Edition2018),
597610
};
598611
}
599612
```
600613

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.
604617

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.
614621

615622
### Renaming or removing a lint
616623

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
618626
to use the old lint name. To declare a rename/remove, add a line with
619627
[`store.register_renamed`] or [`store.register_removed`] to the code of the
620-
[`register_builtins`] function.
628+
[`rustc_lint::register_builtins`] function.
621629

622630
```rust,ignore
623631
store.register_renamed("single_use_lifetime", "single_use_lifetimes");
624632
```
625633

626634
[`store.register_renamed`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/struct.LintStore.html#method.register_renamed
627635
[`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
629637

630638
### Lint Groups
631639

@@ -649,6 +657,14 @@ This defines the `nonstandard_style` group which turns on the listed lints. A
649657
user can turn on these lints with a `!#[warn(nonstandard_style)]` attribute in
650658
the source code, or by passing `-W nonstandard-style` on the command line.
651659

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+
652668
### Linting early in the compiler
653669

654670
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.
682698

683699
The compiler accepts an `--error-format json` flag to output
684700
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:
686702

687703
```console
688704
$ rustc json_error_demo.rs --error-format json

0 commit comments

Comments
 (0)