Skip to content

Commit

Permalink
Inherent method now doc hidden (#821)
Browse files Browse the repository at this point in the history
* Added inherent_method_now_doc_hidden

* Update src/lints/inherent_method_now_doc_hidden.ron

Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com>

* Update src/lints/inherent_method_now_doc_hidden.ron

Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com>

* WIP try doc hidden for struct as well as methods

---------

Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com>
  • Loading branch information
orblivion and obi1kenobi authored Jul 27, 2024
1 parent 781fda5 commit 6173c4f
Show file tree
Hide file tree
Showing 18 changed files with 339 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/lints/inherent_method_now_doc_hidden.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
SemverQuery(
id: "inherent_method_now_doc_hidden",
human_readable_name: "inherent method #[doc(hidden)] added",
description: "A method or associated fn is now marked #[doc(hidden)] and is thus no longer part of the public API.",
required_update: Major,
lint_level: Deny,
reference_link: Some("https://doc.rust-lang.org/rustdoc/write-documentation/the-doc-attribute.html#hidden"),
query: r#"
{
CrateDiff {
baseline {
item {
... on ImplOwner {
visibility_limit @filter(op: "=", value: ["$public"]) @output
importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}
inherent_impl {
method {
method_visibility: visibility_limit @filter(op: "=", value: ["$public"]) @output
method_name: name @output @tag
public_api_eligible @filter(op: "=", value: ["$true"])
span_: span @optional {
filename @output
begin_line @output
}
}
}
}
}
}
current {
item {
... on ImplOwner {
visibility_limit @filter(op: "=", value: ["$public"])
name @output
importable_path {
path @filter(op: "=", value: ["%path"])
public_api @filter(op: "=", value: ["$true"])
}
inherent_impl {
method {
method_visibility: visibility_limit @filter(op: "=", value: ["$public"])
name @filter(op: "=", value: ["%method_name"])
public_api_eligible @filter(op: "!=", value: ["$true"])
}
}
}
}
}
}
}"#,
arguments: {
"public": "public",
"true": true
},
error_message: "A method or associated fn is now #[doc(hidden)], removing it from the crate's public API.",
per_result_error_template: Some("{{name}}::{{method_name}} in file {{span_filename}}:{{span_begin_line}}"),
)
1 change: 1 addition & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ add_lints!(
inherent_method_const_removed,
inherent_method_missing,
inherent_method_must_use_added,
inherent_method_now_doc_hidden,
inherent_method_unsafe_added,
method_parameter_count_changed,
module_missing,
Expand Down
7 changes: 7 additions & 0 deletions test_crates/inherent_method_now_doc_hidden/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "inherent_method_now_doc_hidden"
version = "0.1.0"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Tests false positives. If the struct is #[doc(hidden)], changing hidden state of methods should
// have no effect.

#[doc(hidden)]
pub struct Foo;

impl Foo {
#[doc(hidden)]
pub fn associated_fn(x: i64, y: i64) -> i64 {
x + y
}

#[doc(hidden)]
pub fn method(&self, x: i64) -> i64 {
x
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pub enum Foo {
Bar,
}

impl Foo {
#[doc(hidden)]
pub fn associated_fn(x: i64, y: i64) -> i64 {
x + y
}

#[doc(hidden)]
pub fn method(&self, x: i64) -> i64 {
x
}
}
10 changes: 10 additions & 0 deletions test_crates/inherent_method_now_doc_hidden/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

pub mod union_inherent_method_now_doc_hidden;

pub mod enum_inherent_method_now_doc_hidden;

pub mod struct_inherent_method_now_doc_hidden;

pub mod doc_hidden_struct_inherent_method_now_doc_hidden;

pub mod struct_and_inherent_method_now_doc_hidden;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Tests false positives. If the struct becomes #[doc(hidden)], it should trigger
// struct_now_doc_hidden, and changing hidden state of methods should have no further effect.

#[doc(hidden)]
pub struct Foo;

impl Foo {
#[doc(hidden)]
pub fn associated_fn(x: i64, y: i64) -> i64 {
x + y
}

#[doc(hidden)]
pub fn method(&self, x: i64) -> i64 {
x
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub struct Foo;

impl Foo {
#[doc(hidden)]
pub fn associated_fn(x: i64, y: i64) -> i64 {
x + y
}

#[doc(hidden)]
pub fn method(&self, x: i64) -> i64 {
x
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pub union Foo {
f1: u32,
f2: f32,
}

impl Foo {
#[doc(hidden)]
pub fn associated_fn(x: i64, y: i64) -> i64 {
x + y
}

#[doc(hidden)]
pub fn method(&self, x: i64) -> i64 {
x
}
}
7 changes: 7 additions & 0 deletions test_crates/inherent_method_now_doc_hidden/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "inherent_method_now_doc_hidden"
version = "0.1.0"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Tests false positives. If the struct is #[doc(hidden)], changing hidden state of methods should
// have no effect.

#[doc(hidden)]
pub struct Foo;

impl Foo {
pub fn associated_fn(x: i64, y: i64) -> i64 {
x + y
}

pub fn method(&self, x: i64) -> i64 {
x
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub enum Foo {
Bar,
}

impl Foo {
pub fn associated_fn(x: i64, y: i64) -> i64 {
x + y
}

pub fn method(&self, x: i64) -> i64 {
x
}
}
10 changes: 10 additions & 0 deletions test_crates/inherent_method_now_doc_hidden/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

pub mod union_inherent_method_now_doc_hidden;

pub mod enum_inherent_method_now_doc_hidden;

pub mod struct_inherent_method_now_doc_hidden;

pub mod doc_hidden_struct_inherent_method_now_doc_hidden;

pub mod struct_and_inherent_method_now_doc_hidden;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Tests false positives. If the struct becomes #[doc(hidden)], it should trigger
// struct_now_doc_hidden, and changing hidden state of methods should have no further effect.

pub struct Foo;

impl Foo {
pub fn associated_fn(x: i64, y: i64) -> i64 {
x + y
}

pub fn method(&self, x: i64) -> i64 {
x
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub struct Foo;

impl Foo {
pub fn associated_fn(x: i64, y: i64) -> i64 {
x + y
}

pub fn method(&self, x: i64) -> i64 {
x
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub union Foo {
f1: u32,
f2: f32,
}

impl Foo {
pub fn associated_fn(x: i64, y: i64) -> i64 {
x + y
}

pub fn method(&self, x: i64) -> i64 {
x
}
}
82 changes: 82 additions & 0 deletions test_outputs/inherent_method_now_doc_hidden.output.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"./test_crates/inherent_method_now_doc_hidden/": [
{
"method_name": String("associated_fn"),
"method_visibility": String("public"),
"name": String("Foo"),
"path": List([
String("inherent_method_now_doc_hidden"),
String("enum_inherent_method_now_doc_hidden"),
String("Foo"),
]),
"span_begin_line": Uint64(6),
"span_filename": String("src/enum_inherent_method_now_doc_hidden.rs"),
"visibility_limit": String("public"),
},
{
"method_name": String("method"),
"method_visibility": String("public"),
"name": String("Foo"),
"path": List([
String("inherent_method_now_doc_hidden"),
String("enum_inherent_method_now_doc_hidden"),
String("Foo"),
]),
"span_begin_line": Uint64(10),
"span_filename": String("src/enum_inherent_method_now_doc_hidden.rs"),
"visibility_limit": String("public"),
},
{
"method_name": String("associated_fn"),
"method_visibility": String("public"),
"name": String("Foo"),
"path": List([
String("inherent_method_now_doc_hidden"),
String("struct_inherent_method_now_doc_hidden"),
String("Foo"),
]),
"span_begin_line": Uint64(4),
"span_filename": String("src/struct_inherent_method_now_doc_hidden.rs"),
"visibility_limit": String("public"),
},
{
"method_name": String("method"),
"method_visibility": String("public"),
"name": String("Foo"),
"path": List([
String("inherent_method_now_doc_hidden"),
String("struct_inherent_method_now_doc_hidden"),
String("Foo"),
]),
"span_begin_line": Uint64(8),
"span_filename": String("src/struct_inherent_method_now_doc_hidden.rs"),
"visibility_limit": String("public"),
},
{
"method_name": String("associated_fn"),
"method_visibility": String("public"),
"name": String("Foo"),
"path": List([
String("inherent_method_now_doc_hidden"),
String("union_inherent_method_now_doc_hidden"),
String("Foo"),
]),
"span_begin_line": Uint64(7),
"span_filename": String("src/union_inherent_method_now_doc_hidden.rs"),
"visibility_limit": String("public"),
},
{
"method_name": String("method"),
"method_visibility": String("public"),
"name": String("Foo"),
"path": List([
String("inherent_method_now_doc_hidden"),
String("union_inherent_method_now_doc_hidden"),
String("Foo"),
]),
"span_begin_line": Uint64(11),
"span_filename": String("src/union_inherent_method_now_doc_hidden.rs"),
"visibility_limit": String("public"),
},
],
}
12 changes: 12 additions & 0 deletions test_outputs/struct_now_doc_hidden.output.ron
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
"struct_name": String("StructF"),
},
],
"./test_crates/inherent_method_now_doc_hidden/": [
{
"path": List([
String("inherent_method_now_doc_hidden"),
String("struct_and_inherent_method_now_doc_hidden"),
String("Foo"),
]),
"span_begin_line": Uint64(5),
"span_filename": String("src/struct_and_inherent_method_now_doc_hidden.rs"),
"struct_name": String("Foo"),
},
],
"./test_crates/struct_now_doc_hidden/": [
{
"path": List([
Expand Down

0 comments on commit 6173c4f

Please # to comment.