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

False positive for prefer_self_in_static_references when an inner struct has the same name as its owner #5917

Open
2 tasks done
mildm8nnered opened this issue Dec 27, 2024 · 4 comments
Labels
acceptable-false-positive False positives caused by rules that are unavoidable due to missing type information. bug Unexpected and reproducible misbehavior.

Comments

@mildm8nnered
Copy link
Collaborator

mildm8nnered commented Dec 27, 2024

New Issue Checklist

Bug Description

struct S1 {
    struct S1 {}
    var s = S1()
}

prefer_self_in_static_references will report a violation at var s = S1(), but S1 here refers to the inner struct, not the outer one, so var s= Self() would be different, and would fail if the inner S1 has different properties to the outer one.

As a workaround,

struct S1 {
    struct S1 {}
    var s = Self.S1()
}

Has the same effect, but does not trigger prefer_self_in_static_references.

@mildm8nnered mildm8nnered added the bug Unexpected and reproducible misbehavior. label Dec 27, 2024
@mildm8nnered
Copy link
Collaborator Author

I couldn't see any easy way to fix this :-(

@SimplyDanny
Copy link
Collaborator

SimplyDanny commented Dec 27, 2024

Would Self not refer to the inner struct?

Edit: Seems like the compiler wouldn't even allow it: https://swiftfiddle.com/iclkmj5lbbd4xew7g42r7l4vo4

@mildm8nnered
Copy link
Collaborator Author

Would Self not refer to the inner struct?

Edit: Seems like the compiler wouldn't even allow it: https://swiftfiddle.com/iclkmj5lbbd4xew7g42r7l4vo4

🤦 - I messed up with my example - corrected above.

Here is an example that shows it more clearly I hope

struct Foo {
  struct Foo {
    var bar = 0
  }
  var foo = 0

  func something() -> Int {
    Foo().bar
  }
}

This compiles fine, but

swiftlint --only-rule prefer_self_in_static_references t.swift produces

warning: Prefer Self in Static References Violation: Use Self to refer to the surrounding type name (prefer_self_in_static_references)

@SimplyDanny
Copy link
Collaborator

Got it. This is nothing we can reliably avoid due to missing type information.

@SimplyDanny SimplyDanny added the acceptable-false-positive False positives caused by rules that are unavoidable due to missing type information. label Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
acceptable-false-positive False positives caused by rules that are unavoidable due to missing type information. bug Unexpected and reproducible misbehavior.
Projects
None yet
Development

No branches or pull requests

2 participants