Skip to content

Commit 7c41c41

Browse files
committed
Add config option recursive-self-in-struct
1 parent a541300 commit 7c41c41

File tree

12 files changed

+100
-0
lines changed

12 files changed

+100
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7051,6 +7051,7 @@ Released 2018-09-13
70517051
[`msrv`]: https://doc.rust-lang.org/clippy/lint_configuration.html#msrv
70527052
[`pass-by-value-size-limit`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pass-by-value-size-limit
70537053
[`pub-underscore-fields-behavior`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pub-underscore-fields-behavior
7054+
[`recursive-self-in-struct`]: https://doc.rust-lang.org/clippy/lint_configuration.html#recursive-self-in-struct
70547055
[`semicolon-inside-block-ignore-singleline`]: https://doc.rust-lang.org/clippy/lint_configuration.html#semicolon-inside-block-ignore-singleline
70557056
[`semicolon-outside-block-ignore-multiline`]: https://doc.rust-lang.org/clippy/lint_configuration.html#semicolon-outside-block-ignore-multiline
70567057
[`single-char-binding-names-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#single-char-binding-names-threshold

book/src/lint_configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,16 @@ exported visibility, or whether they are marked as "pub".
927927
* [`pub_underscore_fields`](https://rust-lang.github.io/rust-clippy/master/index.html#pub_underscore_fields)
928928

929929

930+
## `recursive-self-in-struct`
931+
Whether the type itself in a struct or enum should be replaced with `Self` when encountering recursive types.
932+
933+
**Default Value:** `true`
934+
935+
---
936+
**Affected lints:**
937+
* [`use_self`](https://rust-lang.github.io/rust-clippy/master/index.html#use_self)
938+
939+
930940
## `semicolon-inside-block-ignore-singleline`
931941
Whether to lint only if it's multiline.
932942

clippy_config/src/conf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,9 @@ define_Conf! {
809809
/// exported visibility, or whether they are marked as "pub".
810810
#[lints(pub_underscore_fields)]
811811
pub_underscore_fields_behavior: PubUnderscoreFieldsBehaviour = PubUnderscoreFieldsBehaviour::PubliclyExported,
812+
/// Whether the type itself in a struct or enum should be replaced with `Self` when encountering recursive types.
813+
#[lints(use_self)]
814+
recursive_self_in_struct: bool = true,
812815
/// Whether to lint only if it's multiline.
813816
#[lints(semicolon_inside_block)]
814817
semicolon_inside_block_ignore_singleline: bool = false,

clippy_lints/src/use_self.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ declare_clippy_lint! {
5858
pub struct UseSelf {
5959
msrv: Msrv,
6060
stack: Vec<StackItem>,
61+
recursive_self_in_struct: bool,
6162
}
6263

6364
impl UseSelf {
6465
pub fn new(conf: &'static Conf) -> Self {
6566
Self {
6667
msrv: conf.msrv,
6768
stack: Vec::new(),
69+
recursive_self_in_struct: conf.recursive_self_in_struct,
6870
}
6971
}
7072
}
@@ -113,6 +115,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
113115
types_to_skip,
114116
}
115117
} else if let ItemKind::Struct(..) | ItemKind::Enum(..) = item.kind
118+
&& self.recursive_self_in_struct
116119
&& !item.span.from_expansion()
117120
&& !is_from_proc_macro(cx, item)
118121
{

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
6666
msrv
6767
pass-by-value-size-limit
6868
pub-underscore-fields-behavior
69+
recursive-self-in-struct
6970
semicolon-inside-block-ignore-singleline
7071
semicolon-outside-block-ignore-multiline
7172
single-char-binding-names-threshold
@@ -161,6 +162,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
161162
msrv
162163
pass-by-value-size-limit
163164
pub-underscore-fields-behavior
165+
recursive-self-in-struct
164166
semicolon-inside-block-ignore-singleline
165167
semicolon-outside-block-ignore-multiline
166168
single-char-binding-names-threshold
@@ -256,6 +258,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
256258
msrv
257259
pass-by-value-size-limit
258260
pub-underscore-fields-behavior
261+
recursive-self-in-struct
259262
semicolon-inside-block-ignore-singleline
260263
semicolon-outside-block-ignore-multiline
261264
single-char-binding-names-threshold

tests/ui-toml/use_self/default/clippy.toml

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
recursive-self-in-struct = false
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@revisions: default disabled
2+
//@[disabled] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/use_self/disabled
3+
//@[default] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/use_self/default
4+
5+
#![warn(clippy::use_self)]
6+
7+
fn main() {}
8+
9+
struct Basic {
10+
flag: Option<Box<Self>>,
11+
//~[default]^ use_self
12+
}
13+
14+
impl Basic {
15+
fn x(_: Self) {}
16+
//~[default,disabled]^ use_self
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: unnecessary structure name repetition
2+
--> tests/ui-toml/use_self/use_self.rs:10:22
3+
|
4+
LL | flag: Option<Box<Basic>>,
5+
| ^^^^^ help: use the applicable keyword: `Self`
6+
|
7+
= note: `-D clippy::use-self` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::use_self)]`
9+
10+
error: unnecessary structure name repetition
11+
--> tests/ui-toml/use_self/use_self.rs:15:13
12+
|
13+
LL | fn x(_: Basic) {}
14+
| ^^^^^ help: use the applicable keyword: `Self`
15+
16+
error: aborting due to 2 previous errors
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@revisions: default disabled
2+
//@[disabled] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/use_self/disabled
3+
//@[default] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/use_self/default
4+
5+
#![warn(clippy::use_self)]
6+
7+
fn main() {}
8+
9+
struct Basic {
10+
flag: Option<Box<Basic>>,
11+
//~[default]^ use_self
12+
}
13+
14+
impl Basic {
15+
fn x(_: Self) {}
16+
//~[default,disabled]^ use_self
17+
}

0 commit comments

Comments
 (0)