False Positive: `_modify` for analyzer rules
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
Describe the bug
When using the unused_declarations analyze rule, I noticed that _modify blocks are erroneously triggering the analyzer; that would be akin to a get or set block triggering the analyzer.
Note that when we scan it for errors, declarations like this will produce an error on the _modify keyword:
@inlinable public subscript<T: AnyScalarType & Hashable>(_ key: String) -> T {
get {
#if swift(>=5.4)
_data[key] as! T
#else
_data[key]?.base as! T
#endif
}
set { _data[key] = newValue }
_modify {
var value = _data[key] as! T
defer { _data[key] = value }
yield &value
}
}
Complete output when running SwiftLint, including the stack trace and command used
$ ➜ swiftlint analyze --compiler-log-path xcodebuild.log
It's quite long, so I'll just filter it for the errors:
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/GraphQLHTTPMethod.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/Collection+Helpers.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/PossiblyDeferred.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/NormalizedCache.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/SelectionSet+JSONInitializer.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/GraphQLSelectionSetMapper.swift:5:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/RequestChain.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/RequestBodyCreator.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/InMemoryNormalizedCache.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/DataLoader.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/HTTPURLResponse+Helpers.swift:8:7: error: Unused Declaration Violation: Declarations should be referenced at least once within all files linted (unused_declaration)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/DispatchQueue+Optional.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/Apollo/GraphQLResult.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/ApolloAPI/InputValue.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/ApolloAPI/LocalCacheMutation.swift:39:5: error: Unused Declaration Violation: Declarations should be referenced at least once within all files linted (unused_declaration)
/Users/zer0/Developer/OSS/apollo-ios/Sources/ApolloAPI/LocalCacheMutation.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/ApolloAPI/DataDict.swift:35:5: error: Unused Declaration Violation: Declarations should be referenced at least once within all files linted (unused_declaration)
/Users/zer0/Developer/OSS/apollo-ios/Sources/ApolloAPI/DataDict.swift:45:5: error: Unused Declaration Violation: Declarations should be referenced at least once within all files linted (unused_declaration)
/Users/zer0/Developer/OSS/apollo-ios/Sources/ApolloAPI/JSON.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/ApolloAPI/GraphQLNullable.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/ApolloAPI/AnyHashableConvertible.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/ApolloAPI/Selection.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
/Users/zer0/Developer/OSS/apollo-ios/Sources/ApolloAPI/Selection+Conditions.swift:1:1: warning: Unused Import Violation: All imported modules should be required to make the file compile (unused_import)
Environment
- SwiftLint version (run
swiftlint versionto be sure)? 0.52.3 - Installation method used (Homebrew, CocoaPods, building from source, etc)? Brew
- Paste your configuration file:
analyzer_rules:
- unused_import
- unused_declaration
- Which Xcode version are you using (check
xcodebuild -version)? 14.3.1
@inlinable public subscript<T: AnyScalarType & Hashable>(_ key: String) -> T {
get {
#if swift(>=5.4)
_data[key] as! T
#else
_data[key]?.base as! T
#endif
}
set { _data[key] = newValue }
_modify {
var value = _data[key] as! T
defer { _data[key] = value }
yield &value
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running swiftlint analyze --compiler-log-path xcodebuild.log with the unused_declaration analyzer rule against the provided subscript containing _modify. Trace how the analyzer handles get and set blocks versus _modify, then add coverage for the sample and verify that _modify no longer produces a false positive.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100