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 in array_init #3749

Closed
2 tasks done
liam-i opened this issue Oct 29, 2021 · 9 comments
Closed
2 tasks done

False positive in array_init #3749

liam-i opened this issue Oct 29, 2021 · 9 comments
Labels
bug Unexpected and reproducible misbehavior.

Comments

@liam-i
Copy link

liam-i commented Oct 29, 2021

New Issue Checklist

Describe the bug

A clear and concise description of what the bug is.

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint
/Users/liam/Documents/Example/AppDelegate.swift:26:45: Array Init Violation: Prefer using `Array(seq)` over `seq.map { $0 }` to convert a sequence into an Array. (array_init)

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.45.0
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? CocoaPods
  • Paste your configuration file:
only_rules:
  - array_init
  • Are you using nested configurations?
    If so, paste their relative paths and respective contents.
    No

  • Which Xcode version are you using (check xcodebuild -version)?
    Xcode 13.1

  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.

enum BizError: Swift.Error {
}

let reqResult: Result<String, BizError> = .success("")

// False positive in `array_init`
let result: Result<Any, BizError> = reqResult.map { $0 }

print(result)
@aranasaurus
Copy link

I spent some time looking at this today, and I could be wrong since this is my first time jumping into the codebase, but it looks like the way to fix this issue would require changing the ArrayInitRule's KindType to SwiftDeclarationKind, but that would make this rule stop being able to detect occurrences of .map instead of Array() as arguments to function calls.

I couldn't see a way to make one ASTRule able to operate on more than one KindType. Maybe there's another way that I'm just missing?

@jpsim jpsim added the bug Unexpected and reproducible misbehavior. label Jan 20, 2022
@SimplyDanny
Copy link
Collaborator

My take on this is that it cannot be fixed without knowing the type of the object at which .map is called. By default, SwiftLint deals with an AST that is not type checked. I only recently learned that there are a few so called "Analyzer Rules" which have a type checked AST available to work on. They are currently an experimental feature, though.

@jpsim
Copy link
Collaborator

jpsim commented Mar 9, 2022

They are currently an experimental feature, though.

Analyzer rules have been pretty stable and we could remove the "experimental" label on them.

@jpsim
Copy link
Collaborator

jpsim commented Mar 9, 2022

#3884

@SimplyDanny
Copy link
Collaborator

So, is it okay to convert the array_init rule to an analysis rule to fix the bug, @jpsim? If yes, I could have a look into this.

@jpsim
Copy link
Collaborator

jpsim commented Mar 10, 2022

You could make a new analyzer rule that does what array_init tries to do, but more reliably.

However, I wouldn’t remove the current array_init rule due because of what I explained in #3884 (comment).

@SimplyDanny
Copy link
Collaborator

That's what I also thought after reading your comment. Let me give it a try.

@SimplyDanny
Copy link
Collaborator

Can this one be closed now that there is TypesafeArrayInitRule available, @jpsim? I mean, the bug is still valid but certainly won't be fixed.

@jpsim
Copy link
Collaborator

jpsim commented Mar 29, 2022

Yes let's close this, if someone wants a type-checked version of the rule they can use TypesafeArrayInitRule, albeit requiring the compilation database.

@jpsim jpsim closed this as completed Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected and reproducible misbehavior.
Projects
None yet
Development

No branches or pull requests

4 participants