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

empty_enum_arguments false positive with static func #3294

Closed
2 tasks done
keith opened this issue Aug 10, 2020 · 7 comments
Closed
2 tasks done

empty_enum_arguments false positive with static func #3294

keith opened this issue Aug 10, 2020 · 7 comments
Labels
bug Unexpected and reproducible misbehavior. wontfix Issues that became stale and were auto-closed by a bot.

Comments

@keith
Copy link
Collaborator

keith commented Aug 10, 2020

New Issue Checklist

Describe the bug

With this code:

struct Foo: Equatable {
    static func empty() -> Foo {
        return Foo()
    }
}

func bar() {
    let foo = Foo()
    if foo == .empty() {
        print("empty")
    }
}

And this config:

whitelist_rules:
  - empty_enum_arguments

.empty() is falsely marked as something where () can be removed. But those are required to build:

foo.swift:9:16: error: member 'empty()' is a function; did you mean to call it?
    if foo == .empty {
               ^
                    ()
Complete output when running SwiftLint, including the stack trace and command used
swiftlint lint --config foo.yml --path foo.swift
foo.swift:9:21: warning: Empty Enum Arguments Violation: Arguments can be omitted when matching enums with associated types if they are not used. (empty_enum_arguments)

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.40.0
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? portable_swiftlint.zip
  • Paste your configuration file: above

related to #3103 and #3122

@keith
Copy link
Collaborator Author

keith commented Aug 10, 2020

cc @marcelofabri @lordzsolt

@jpsim
Copy link
Collaborator

jpsim commented Aug 10, 2020

This is a regression from 0.39.2, which did not report any violations with the provided repro steps.

@jpsim jpsim added the bug Unexpected and reproducible misbehavior. label Aug 10, 2020
@jpsim
Copy link
Collaborator

jpsim commented Aug 10, 2020

Introduced in #3122

@lordzsolt
Copy link
Contributor

I'll take a look.

@lordzsolt
Copy link
Contributor

lordzsolt commented Aug 16, 2020

I tried fixing it, however I did not find any solution achievable through a simple regex change.

Attempt 1: Require to be preceded by case

Regex: case.*\.\S+\s*(\([,\s_]*\))

However, this fails when there is more than one case per line: case .bar↓(_), .bar2↓(_). This is very bad.


Attempt 2: Require at least 1 _ character between the parenthesis.

Regex: \.\S+\s*(\([,\s]*[,\s_]+[,\s]*\))

But this fails on an earlier example case .bar↓()

However, the only compiling code which contains case .bar↓(), that I could come up with is:

enum Foo {
	case bar(Void)
}

let foo: Foo
switch foo {
	case .bar(): break
}

Because in case of a Void type, you are allowed write either .bar() or .bar(_)

Any non-Void type will fail to compile with the error Tuple pattern cannot match values of the non-tuple type 'T'


Considering I don't think I've ever seen an enum-case with a Void type associated value, I would propose removing case .bar↓() and case .bar↓() where method() > 2 from the triggering examples and treat it as false-negative.

Otherwise, I am not sure how to determine that .empty() is a static function, rather than an enum-case. Does the AST contain such information?

@jpsim
Copy link
Collaborator

jpsim commented Nov 7, 2020

Otherwise, I am not sure how to determine that .empty() is a static function, rather than an enum-case. Does the AST contain such information?

Only the post-typechecked AST, which Analyzer Rules operate on, but not standard lint rules.

@stale
Copy link

stale bot commented Jan 6, 2021

This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!

@stale stale bot added the wontfix Issues that became stale and were auto-closed by a bot. label Jan 6, 2021
@stale stale bot closed this as completed Jan 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected and reproducible misbehavior. wontfix Issues that became stale and were auto-closed by a bot.
Projects
None yet
Development

No branches or pull requests

3 participants