Skip to content

Commit 8db4caa

Browse files
committed
Add config option recursive-self-in-struct
1 parent 9df86f6 commit 8db4caa

File tree

5 files changed

+20
-0
lines changed

5 files changed

+20
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6892,6 +6892,7 @@ Released 2018-09-13
68926892
[`msrv`]: https://doc.rust-lang.org/clippy/lint_configuration.html#msrv
68936893
[`pass-by-value-size-limit`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pass-by-value-size-limit
68946894
[`pub-underscore-fields-behavior`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pub-underscore-fields-behavior
6895+
[`recursive-self-in-struct`]: https://doc.rust-lang.org/clippy/lint_configuration.html#recursive-self-in-struct
68956896
[`semicolon-inside-block-ignore-singleline`]: https://doc.rust-lang.org/clippy/lint_configuration.html#semicolon-inside-block-ignore-singleline
68966897
[`semicolon-outside-block-ignore-multiline`]: https://doc.rust-lang.org/clippy/lint_configuration.html#semicolon-outside-block-ignore-multiline
68976898
[`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_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_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_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

0 commit comments

Comments
 (0)