Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New lint: Trait method #[must_use] added #284

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions test_crates/trait_method_must_use_added/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_method_must_use_added"
version = "0.1.0"
edition = "2021"

[dependencies]
166 changes: 166 additions & 0 deletions test_crates/trait_method_must_use_added/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
pub trait TraitWithDeclaredMustUseMethods {

// These methods did not have the #[must_use] attribute in the old version.
// Addition of the attribute should be reported.

#[must_use]
fn DeclaredMethodToDeclaredMustUseMethod(&self);

#[must_use = "Foo"]
fn DeclaredMethodToDeclaredMustUseMessageMethod(&self);


// These methods had the #[must_use] attribute in the old version. Changes of
// the attribute, including deletion, should not be reported.

fn DeclaredMustUseMethodToDeclaredMethod(&self);

#[must_use = "Foo"]
fn DeclaredMustUseMethodToDeclaredMustUseMessageMethod(&self);


// These methods had the #[must_use] attribute in the old version.
// They also included the user-defined warning message. Changes of
// the attribute, including deletion, should not be reported.

fn DeclaredMustUseMessageMethodToDeclaredMethod(&self);

#[must_use]
fn DeclaredMustUseMessageMethodToDeclaredMustUseMethod(&self);

#[must_use = "Baz"]
fn DeclaredMustUseMessageMethodToDeclaredMustUseMessageMethod(&self);
}


pub trait TraitWithProvidedMustUseMethods {

// These methods did not have the #[must_use] attribute in the old version.
// Addition of the attribute should be reported.

#[must_use]
fn ProvidedMethodToProvidedMustUseMethod(&self) {}

#[must_use = "Foo"]
fn ProvidedMethodToProvidedMustUseMessageMethod(&self) {}


// These methods had the #[must_use] attribute in the old version. Changes of
// the attribute, including deletion, should not be reported.

fn ProvidedMustUseMethodToProvidedMethod(&self) {}

#[must_use = "Foo"]
fn ProvidedMustUseMethodToProvidedMustUseMessageMethod(&self) {}


// These methods had the #[must_use] attribute in the old version.
// They also included the user-defined warning message. Changes of
// the attribute, including deletion, should not be reported.

fn ProvidedMustUseMessageMethodToProvidedMethod(&self) {}

#[must_use]
fn ProvidedMustUseMessageMethodToProvidedMustUseMethod(&self) {}

#[must_use = "Baz"]
fn ProvidedMustUseMessageMethodToProvidedMustUseMessageMethod(&self) {}
}


pub trait TraitWithDeclaredToProvidedMustUseMethods {

// These methods did not have the #[must_use] attribute in the old version.
// Addition of the attribute should be reported.

#[must_use]
fn DeclaredMethodToProvidedMustUseMethod(&self) {}

#[must_use = "Foo"]
fn DeclaredMethodToProvidedMustUseMessageMethod(&self) {}


// These methods had the #[must_use] attribute in the old version. Changes of
// the attribute, including deletion, should not be reported.

fn DeclaredMustUseMethodToProvidedMethod(&self) {}

#[must_use = "Foo"]
fn DeclaredMustUseMethodToProvidedMustUseMessageMethod(&self) {}


// These methods had the #[must_use] attribute in the old version.
// They also included the user-defined warning message. Changes of
// the attribute, including deletion, should not be reported.

fn DeclaredMustUseMessageMethodToProvidedMethod(&self) {}

#[must_use]
fn DeclaredMustUseMessageMethodToProvidedMustUseMethod(&self) {}

#[must_use = "Baz"]
fn DeclaredMustUseMessageMethodToProvidedMustUseMessageMethod(&self) {}
}


// This trait's methods were provided in the old version of the crate, but
// their bodies have been removed turning them into declared methods.
// They should NOT be reported by this rule to avoid duplicate lints.
// They should be reported as changes of the trait's provided methods into
// declared methods.

pub trait TraitWithProvidedToDeclaredMustUseMethods {

#[must_use]
fn ProvidedMethodToDeclaredMustUseMethod(&self);

#[must_use = "Foo"]
fn ProvidedMethodToDeclaredMustUseMessageMethod(&self);

fn ProvidedMustUseMethodToDeclaredMethod(&self);

#[must_use = "Foo"]
fn ProvidedMustUseMethodToDeclaredMustUseMessageMethod(&self);

fn ProvidedMustUseMessageMethodToDeclaredMethod(&self);

#[must_use]
fn ProvidedMustUseMessageMethodToDeclaredMustUseMethod(&self);

#[must_use = "Baz"]
fn ProvidedMustUseMessageMethodToDeclaredMustUseMessageMethod(&self);
}


// This trait is private and adding #[must_use] to its methods
// should NOT be reported.

trait PrivateTraitWithMustUseMethods {

#[must_use]
fn PrivateDeclaredMethodToPrivateDeclaredMustUseMethod(&self);

#[must_use]
fn PrivateProvidedMethodToPrivateProvidedMustUseMethod(&self) {}

#[must_use]
fn PrivateDeclaredMethodToPrivateProvidedMustUseMethod(&self) {}

#[must_use]
fn PrivateProvidedMethodToPrivateDeclaredMustUseMethod(&self);
}


// This trait and its methods were added in the new version of the crate,
// together with the methods' attributes.
// It should NOT be reported by this rule to avoid duplicate lints.
// It should be reported as a new pub type that is part of the crate's API.

pub trait NewTraitWithMustUseMethods {

#[must_use]
fn NewTraitWithDeclaredMustUseMethod(&self);

#[must_use]
fn NewTraitWithProvidedMustUseMethod(&self) {}
}
7 changes: 7 additions & 0 deletions test_crates/trait_method_must_use_added/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_method_must_use_added"
version = "0.1.0"
edition = "2021"

[dependencies]
147 changes: 147 additions & 0 deletions test_crates/trait_method_must_use_added/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
pub trait TraitWithDeclaredMustUseMethods {

// These methods did not have the #[must_use] attribute in the old version.
// Addition of the attribute should be reported.

fn DeclaredMethodToDeclaredMustUseMethod(&self);

fn DeclaredMethodToDeclaredMustUseMessageMethod(&self);


// These methods had the #[must_use] attribute in the old version. Changes
// of the attribute, including deletion, should not be reported.

#[must_use]
fn DeclaredMustUseMethodToDeclaredMethod(&self);

#[must_use]
fn DeclaredMustUseMethodToDeclaredMustUseMessageMethod(&self);


// These methods had the #[must_use] attribute in the old version.
// They also included the user-defined warning message. Changes of
// the attribute, including deletion, should not be reported.

#[must_use = "Foo"]
fn DeclaredMustUseMessageMethodToDeclaredMethod(&self);

#[must_use = "Foo"]
fn DeclaredMustUseMessageMethodToDeclaredMustUseMethod(&self);

#[must_use = "Foo"]
fn DeclaredMustUseMessageMethodToDeclaredMustUseMessageMethod(&self);
}


pub trait TraitWithProvidedMustUseMethods {

// These methods did not have the #[must_use] attribute in the old version.
// Addition of the attribute should be reported.

fn ProvidedMethodToProvidedMustUseMethod(&self) {}

fn ProvidedMethodToProvidedMustUseMessageMethod(&self) {}


// These methods had the #[must_use] attribute in the old version. Changes
// of the attribute, including deletion, should not be reported.

#[must_use]
fn ProvidedMustUseMethodToProvidedMethod(&self) {}

#[must_use]
fn ProvidedMustUseMethodToProvidedMustUseMessageMethod(&self) {}


// These methods had the #[must_use] attribute in the old version.
// They also included the user-defined warning message. Changes of
// the attribute, including deletion, should not be reported.

#[must_use = "Foo"]
fn ProvidedMustUseMessageMethodToProvidedMethod(&self) {}

#[must_use = "Foo"]
fn ProvidedMustUseMessageMethodToProvidedMustUseMethod(&self) {}

#[must_use = "Foo"]
fn ProvidedMustUseMessageMethodToProvidedMustUseMessageMethod(&self) {}
}


pub trait TraitWithDeclaredToProvidedMustUseMethods {

// These methods did not have the #[must_use] attribute in the old version.
// Addition of the attribute should be reported.

fn DeclaredMethodToProvidedMustUseMethod(&self);

fn DeclaredMethodToProvidedMustUseMessageMethod(&self);


// These methods had the #[must_use] attribute in the old version. Changes
// of the attribute, including deletion, should not be reported.

#[must_use]
fn DeclaredMustUseMethodToProvidedMethod(&self);

#[must_use]
fn DeclaredMustUseMethodToProvidedMustUseMessageMethod(&self);


// These methods had the #[must_use] attribute in the old version.
// They also included the user-defined warning message. Changes of
// the attribute, including deletion, should not be reported.

#[must_use = "Foo"]
fn DeclaredMustUseMessageMethodToProvidedMethod(&self);

#[must_use = "Foo"]
fn DeclaredMustUseMessageMethodToProvidedMustUseMethod(&self);

#[must_use = "Foo"]
fn DeclaredMustUseMessageMethodToProvidedMustUseMessageMethod(&self);
}


// This trait's methods were provided in the old version of the crate, but
// their bodies have been removed turning them into declared methods.
// They should NOT be reported by this rule to avoid duplicate lints.
// They should be reported as changes of the trait's provided methods into
// declared methods.

pub trait TraitWithProvidedToDeclaredMustUseMethods {

fn ProvidedMethodToDeclaredMustUseMethod(&self) {}

fn ProvidedMethodToDeclaredMustUseMessageMethod(&self) {}

#[must_use]
fn ProvidedMustUseMethodToDeclaredMethod(&self) {}

#[must_use]
fn ProvidedMustUseMethodToDeclaredMustUseMessageMethod(&self) {}

#[must_use = "Foo"]
fn ProvidedMustUseMessageMethodToDeclaredMethod(&self) {}

#[must_use = "Foo"]
fn ProvidedMustUseMessageMethodToDeclaredMustUseMethod(&self) {}

#[must_use = "Foo"]
fn ProvidedMustUseMessageMethodToDeclaredMustUseMessageMethod(&self) {}
}


// This trait is private and adding #[must_use] to its methods
// should NOT be reported.

trait PrivateTraitWithMustUseMethods {

fn PrivateDeclaredMethodToPrivateDeclaredMustUseMethod(&self);

fn PrivateProvidedMethodToPrivateProvidedMustUseMethod(&self) {}

fn PrivateDeclaredMethodToPrivateProvidedMustUseMethod(&self);

fn PrivateProvidedMethodToPrivateDeclaredMustUseMethod(&self) {}
}