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

init_numbered_fields incorrectly triggers for type alias of tuple struct #8638

Closed
nyospe opened this issue Apr 6, 2022 · 2 comments · Fixed by #8780
Closed

init_numbered_fields incorrectly triggers for type alias of tuple struct #8638

nyospe opened this issue Apr 6, 2022 · 2 comments · Fixed by #8780
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@nyospe
Copy link

nyospe commented Apr 6, 2022

Summary

The clippy lint init_numbered_fields introduced in 1.59.0 is emitted with an incorrect suggested fix when the struct initialization is for a type alias to a tuple or unit struct.

This case should be omitted from the lint entirely, as there is no means other than use of numbered fields to initialize a tuple struct for which only the type alias can be reliably known, because the type alias cannot be used as a constructor.

Lint Name

init_numbered_fields

Reproducer

I tried this code:

fn main() {
    let _ = TypeAlias { 0: 0 };
}


struct AkwardlyNamedAndPossiblyCfgGatedTupleStruct(i32);

type TypeAlias = AkwardlyNamedAndPossiblyCfgGatedTupleStruct;

I ran cargo clippy

I saw this happen:

warning: used a field initializer for a tuple struct
 --> src/main.rs:2:13
  |
2 |     let _ = TypeAlias { 0: 0 };
  |             ^^^^^^^^^^^^^^^^^^ help: try this instead: `TypeAlias(0)`
  |
  = note: `#[warn(clippy::init_numbered_fields)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#init_numbered_fields

warning: `test_clippy_error` (bin "test_clippy_error") generated 1 warning

I expected to see this happen:

<no clippy warning>

because "A type alias to a tuple-struct or unit-struct cannot be used to qualify that type's constructor:", this should not trigger when the tuple struct initialization is being invoked through a type alias.

Version

rustc 1.59.0 (9d1b2106e 2022-02-23)
binary: rustc
commit-hash: 9d1b2106e23b1abd32fce1f17267604a5102f57a
commit-date: 2022-02-23
host: x86_64-apple-darwin
release: 1.59.0
LLVM version: 13.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@nyospe nyospe added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Apr 6, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 6, 2022
@nyospe
Copy link
Author

nyospe commented Apr 14, 2022

Confirmed still present in 1.60.0

@nyospe
Copy link
Author

nyospe commented Apr 14, 2022

Note: introduced by #8170

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants