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

Add lint for trait default impl removed #880

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
62 changes: 62 additions & 0 deletions src/lints/trait_default_impl_removed.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
SemverQuery(
id: "trait_default_impl_removed",
human_readable_name: "pub trait default method impl removed",
description: "A non-sealed public trait default method impl was removed",
required_update: Major,
lint_level: Deny,
reference_link: Some("https://doc.rust-lang.org/book/ch10-02-traits.html#default-implementations"),
query: r#"
{
CrateDiff {
baseline {
item {
... on Trait {
visibility_limit @filter(op: "=", value: ["$public"]) @output
sealed @filter(op: "!=", value: ["$true"])
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
name @output

importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}

method {
method_name: name @output @tag
public_api_eligible @filter(op: "=", value: ["$true"])
has_body @filter(op: "=", value: ["$true"])
}
}
}
}
current {
item {
... on Trait {
visibility_limit @filter(op: "=", value: ["$public"])
sealed @filter(op: "!=", value: ["$true"])
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved

importable_path {
path @filter(op: "=", value: ["%path"])
public_api @filter(op: "=", value: ["$true"])
}

method {
public_api_eligible @filter(op: "=", value: ["$true"])
name @filter(op: "=", value: ["%method_name"])
has_body @filter(op: "!=", value: ["$true"])
span_: span @optional {
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
filename @output
begin_line @output
}
}
}
}
}
}
}"#,
arguments: {
"true": true,
"public": "public",
},
error_message: "A trait's method default impl was removed, breaking users relying on the default impl",
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
per_result_error_template: Some("trait method <{{join \"::\" path}}>::{{method_name}} in file {{span_filename}}:{{span_begin_line}}"),
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
)
1 change: 1 addition & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ add_lints!(
struct_with_pub_fields_changed_type,
trait_associated_const_now_doc_hidden,
trait_associated_type_now_doc_hidden,
trait_default_impl_removed,
trait_method_missing,
trait_method_now_doc_hidden,
trait_method_unsafe_added,
Expand Down
7 changes: 7 additions & 0 deletions test_crates/trait_default_impl_removed/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_default_impl_removed"
version = "0.1.0"
edition = "2021"

[dependencies]
18 changes: 18 additions & 0 deletions test_crates/trait_default_impl_removed/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Default trait method impl is removed, should be reported
pub trait TraitA {
fn method_default_impl_removed(&self);
}

// Default trait method becomes removed completely, should not be reported as it becomes
// missing instead
pub trait TraitB {}

//Default trait method impl is removed, but its private, shouldnt be reported
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
trait TraitC {
fn method_default_impl_removed(&self);
}

// Non-object safe trait has its default impl removed, should be reported
pub trait TraitD {
fn method_becomes_non_obj_safe(&self) -> Self;
}
7 changes: 7 additions & 0 deletions test_crates/trait_default_impl_removed/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_default_impl_removed"
version = "0.1.0"
edition = "2021"

[dependencies]
20 changes: 20 additions & 0 deletions test_crates/trait_default_impl_removed/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Default trait method impl is removed, should be reported
pub trait TraitA {
fn method_default_impl_removed(&self) {}
}

// Default trait method becomes removed completely, should not be reported as it becomes
// missing instead
pub trait TraitB {
fn method_becomes_removed(&self) {}
}

//Default trait method impl is removed, but its private, shouldnt be reported
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
trait TraitC {
fn method_default_impl_removed(&self) {}
}

// Non-object safe trait has its default impl removed, should be reported
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
pub trait TraitD {
fn method_becomes_non_obj_safe(&self) {}
}
39 changes: 39 additions & 0 deletions test_outputs/trait_default_impl_removed.output.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"./test_crates/trait_default_impl_removed/": [
{
"method_name": String("method_default_impl_removed"),
"name": String("TraitA"),
"path": List([
String("trait_default_impl_removed"),
String("TraitA"),
]),
"span_begin_line": Uint64(3),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
{
"method_name": String("method_becomes_non_obj_safe"),
"name": String("TraitD"),
"path": List([
String("trait_default_impl_removed"),
String("TraitD"),
]),
"span_begin_line": Uint64(17),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
"./test_crates/trait_no_longer_object_safe/": [
{
"method_name": String("by_ref"),
"name": String("RefTrait"),
"path": List([
String("trait_no_longer_object_safe"),
String("RefTrait"),
]),
"span_begin_line": Uint64(3),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
}
13 changes: 13 additions & 0 deletions test_outputs/trait_method_missing.output.ron
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
{
"./test_crates/trait_default_impl_removed/": [
{
"method_name": String("method_becomes_removed"),
"name": String("TraitB"),
"path": List([
String("trait_default_impl_removed"),
String("TraitB"),
]),
"span_begin_line": Uint64(9),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
"./test_crates/trait_method_missing/": [
{
"method_name": String("fooA"),
Expand Down
12 changes: 12 additions & 0 deletions test_outputs/trait_no_longer_object_safe.output.ron
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
"visibility_limit": String("public"),
},
],
"./test_crates/trait_default_impl_removed/": [
{
"name": String("TraitD"),
"path": List([
String("trait_default_impl_removed"),
String("TraitD"),
]),
"span_begin_line": Uint64(16),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
"./test_crates/trait_no_longer_object_safe/": [
{
"name": String("RefTrait"),
Expand Down
Loading