-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#![warn(clippy::missing_must_use_on_future_types)] | ||
#![allow(path_statements, unused, clippy::no_effect)] | ||
|
||
use std::fmt::Display; | ||
use std::future::Future; | ||
use std::marker::PhantomData; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
// basic struct case | ||
#[must_use] | ||
struct BasicStruct; | ||
|
||
impl Future for BasicStruct { | ||
type Output = (); | ||
|
||
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
Poll::Ready(()) | ||
} | ||
} | ||
|
||
// basic enum case | ||
#[must_use] | ||
enum BasicEnum { | ||
Var1, | ||
Var2, | ||
} | ||
|
||
impl Future for BasicEnum { | ||
type Output = (); | ||
|
||
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
Poll::Ready(()) | ||
} | ||
} | ||
|
||
// should be ignored if type doesn't implement `Future` | ||
struct NonFuture; | ||
|
||
// should still trigger if a type only sometimes implements `Future` | ||
#[must_use] | ||
struct SometimesFuture<T>(PhantomData<T>); | ||
|
||
impl<T: Copy> Future for SometimesFuture<T> { | ||
type Output = (); | ||
|
||
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
Poll::Ready(()) | ||
} | ||
} | ||
|
||
// should be ignored on trait objects | ||
trait Trait {} | ||
|
||
impl Future for dyn Trait { | ||
type Output = (); | ||
|
||
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
Poll::Ready(()) | ||
} | ||
} | ||
|
||
struct TraitImpl {} | ||
impl Trait for TraitImpl {} | ||
|
||
fn trait_obj() -> Box<dyn Trait> { | ||
Box::new(TraitImpl {}) | ||
} | ||
|
||
// struct with multiple fields and impls | ||
#[derive(Debug)] | ||
#[must_use] | ||
pub struct ComplexStruct { | ||
x: usize, | ||
y: &'static str, | ||
} | ||
|
||
impl ComplexStruct { | ||
fn sum(&self) -> usize { | ||
self.x + self.y.len() | ||
} | ||
} | ||
|
||
impl Display for ComplexStruct { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "complex") | ||
} | ||
} | ||
|
||
impl Future for ComplexStruct { | ||
type Output = (); | ||
|
||
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
Poll::Ready(()) | ||
} | ||
} | ||
|
||
// should be ignored on already #[must_use] struct | ||
#[must_use] | ||
struct AlreadyMustUse; | ||
|
||
impl Future for AlreadyMustUse { | ||
type Output = (); | ||
|
||
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
Poll::Ready(()) | ||
} | ||
} | ||
|
||
fn main() { | ||
BasicStruct; | ||
BasicEnum::Var2; | ||
NonFuture; | ||
SometimesFuture::<String>(PhantomData); | ||
trait_obj(); | ||
ComplexStruct { x: 42, y: "complex" }; | ||
AlreadyMustUse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
error: this struct implements `Future` but is missing a `#[must_use]` attribute | ||
--> tests/ui/missing_must_use_on_future_types.rs:11:1 | ||
| | ||
LL | struct BasicStruct; | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::missing-must-use-on-future-types` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::missing_must_use_on_future_types)]` | ||
help: add the attribute | ||
| | ||
LL + #[must_use] | ||
LL | struct BasicStruct; | ||
| | ||
|
||
error: this struct implements `Future` but is missing a `#[must_use]` attribute | ||
--> tests/ui/missing_must_use_on_future_types.rs:22:1 | ||
| | ||
LL | enum BasicEnum { | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
help: add the attribute | ||
| | ||
LL + #[must_use] | ||
LL | enum BasicEnum { | ||
| | ||
|
||
error: this struct implements `Future` but is missing a `#[must_use]` attribute | ||
--> tests/ui/missing_must_use_on_future_types.rs:39:1 | ||
| | ||
LL | struct SometimesFuture<T>(PhantomData<T>); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: add the attribute | ||
| | ||
LL + #[must_use] | ||
LL | struct SometimesFuture<T>(PhantomData<T>); | ||
| | ||
|
||
error: this struct implements `Future` but is missing a `#[must_use]` attribute | ||
--> tests/ui/missing_must_use_on_future_types.rs:69:1 | ||
| | ||
LL | pub struct ComplexStruct { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: add the attribute | ||
| | ||
LL + #[must_use] | ||
LL | pub struct ComplexStruct { | ||
| | ||
|
||
error: aborting due to 4 previous errors | ||
|