realm / realm/SwiftLint

`empty_string` triggers in cases where the left hand side isn't a string.

Open
#4,249 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

acceptable-false-positive bug
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
Describe the bug

On main (fd7afedfcfe73443a95041fdc51c31d071b97910), the empty_string rule seems to check if the rhs of an equality operator is an empty string literal. This can give false positives when the lhs isn't a string.

It fails in a test using Nimble matchers comparing to an empty string. In this case the suggestion doesn't make sense since .isEmpty isn't available on the type on the lhs.

expect(outputText) == ""

Looking at the implementation, it may also give false positives for types that are StringLiteralConvertible. The suggestions of using isEmpty also wouldn't be possible in that case.

Though, maybe the lint warning message should be changed from assuming the lhs is a string.

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint
path/to/file: [warning:
Empty String Violation: Prefer checking `isEmpty` over comparing `string` to an empty string literal. (empty_string)](warning: Empty String Violation: Prefer checking `isEmpty` over comparing `string` to an empty string literal. (empty_string))
Done linting! Found 1 violations, 0 serious in 1 file.
Environment
  • SwiftLint version (run swiftlint version to be sure)? 0.49.1 (main)
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? building from source
  • Paste your configuration file:
opt_in_rules:
  - empty_string
  • Are you using nested configurations? No
    If so, paste their relative paths and respective contents.
  • Which Xcode version are you using (check xcodebuild -version)? Xcode 14.0.1
  • Do you have a sample that shows the issue?
echo "expect(outputText) == \"\"" | swiftlint lint --no-cache --use-stdin --enable-all-rules

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the empty_string rule implementation and reproduce the report using swiftlint lint --no-cache --use-stdin --enable-all-rules with the Nimble-style comparison. Check the rule's handling of equality operands and verify that non-string left-hand sides no longer receive the isEmpty suggestion while string comparisons retain the intended warning.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.