SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

False positive for `unused_enumerated`

Open carl8382 opened this issue 1 year ago • 3 comments

New Issue Checklist

Bug Description

There's a false positive for the rule unused_enumerated. The offset given from enumerated is used, just apparently not fast enough.

// This triggers a unused_enumerated  violation:
let previousValueComponent = "1234"
let currentValueComponent = "1434"

                // Compare previous and current components and find if there are any indexes where:
                // - two characters are digits
                // - these two digits are different
                //
                let firstIndexWithDifferentDigit = zip(previousValueComponent, currentValueComponent)
                    .enumerated()
                    .first {
                        $0.element.0.isNumber &&
                        $0.element.1.isNumber &&
                        $0.element.0 != $0.element.1
                    }?.offset

                return firstIndexWithDifferentDigit

Mention the command or other SwiftLint integration method that caused the issue. Include stack traces or command output.

$ swiftlint lint [--no-cache] [--fix]

Environment

  • SwiftLint version (run swiftlint version to be sure) 0.57.1
  • Xcode version (run xcodebuild -version to be sure) 16.0
  • Installation method used (Homebrew, CocoaPods, building from source, etc) SPM
  • Configuration file:
# insert yaml contents here

Are you using nested configurations? If so, paste their relative paths and respective contents.

carl8382 avatar Dec 03 '24 20:12 carl8382

For reference, here's an even simpler code snippet that triggers a false positive:

public extension Array where Element: Comparable {
	var maxIndex: Int? {
		self.enumerated().max { $0.element < $1.element }?.offset
	}
}

Just because offset isn't used in the closure doesn't mean that .enumerated() can be removed, as the warning message incorrectly suggests.

ortekka avatar Jan 30 '25 10:01 ortekka

Is this scheduled to be fixed? Thanks.

rgoldberg avatar Oct 20 '25 22:10 rgoldberg

Is this scheduled to be fixed? Thanks.

Well, we don't schedule topics. Whoever wants to, may work on it.

SimplyDanny avatar Oct 22 '25 08:10 SimplyDanny