Skip to content

Commit 09de1df

Browse files
committed
Add documentation for #[diagnostic::do_not_recommend]
1 parent 23ce619 commit 09de1df

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/attributes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ The following is an index of all built-in attributes.
239239
- [`must_use`] --- Generates a lint for unused values.
240240
- [`diagnostic::on_unimplemented`] --- Hints the compiler to emit a certain error
241241
message if a trait is not implemented.
242+
- [`diagnostic::do_not_recommend`] --- Hints the compiler to not show a certain trait
243+
impl in error messages
242244
- ABI, linking, symbols, and FFI
243245
- [`link`] --- Specifies a native library to link with an `extern` block.
244246
- [`link_name`] --- Specifies the name of the symbol for functions or statics
@@ -371,3 +373,4 @@ The following is an index of all built-in attributes.
371373
[function pointer]: types/function-pointer.md
372374
[variadic functions]: items/external-blocks.html#variadic-functions
373375
[`diagnostic::on_unimplemented`]: attributes/diagnostics.md#the-diagnosticon_unimplemented-attribute
376+
[`diagnostic::do_not_recommend`]: attributes/diagnostics.md#the-diagnosticdo_not_recommend-attribute

src/attributes/diagnostics.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,47 @@ error[E0277]: My Message for `ImportantTrait<i32>` implemented for `String`
492492
= note: Note 2
493493
```
494494

495+
### The `diagnostic::do_not_recommend` attribute
496+
497+
The `#[diagnostic::on_unimplemented]` attribute is a hint to the compiler to not show a certain trait implementation as part of the error message.
498+
The attribute should be placed on a [trait implementation items]. It does not accept any arguments. If the attribute is placed in a wrong location or contains an unexpected argument the compiler might emit a warning and otherwise ignore the malformed part.
499+
500+
In this example:
501+
502+
```rust,compile_fail,E0277
503+
trait Foo {}
504+
505+
#[diagnostic::do_not_recommend]
506+
impl<T> Foo for T where T: Send {}
507+
508+
fn needs_foo<T: Foo>() {}
509+
510+
fn main() {
511+
needs_foo::<*mut ()>();
512+
}
513+
514+
```
515+
516+
the compiler may generate an error message which looks like this:
517+
518+
```text
519+
error[E0277]: the trait bound `*mut (): Foo` is not satisfied
520+
--> $DIR/simple.rs:15:17
521+
|
522+
LL | needs_foo::<*mut ()>();
523+
| ^^^^^^^ the trait `Foo` is not implemented for `*mut ()`
524+
|
525+
note: required by a bound in `needs_foo`
526+
--> $DIR/simple.rs:10:17
527+
|
528+
LL | fn needs_foo<T: Foo>() {}
529+
| ^^^ required by this bound in `needs_foo`
530+
531+
error: aborting due to 1 previous error
532+
```
533+
534+
Without the attribute the compiler would complain about `*mut (): Send` instead.
535+
495536
[Clippy]: https://github.com/rust-lang/rust-clippy
496537
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
497538
[_MetaListPaths_]: ../attributes.md#meta-item-attribute-syntax

0 commit comments

Comments
 (0)