SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Superfluous warning for shorthand_operator disable

Open mgray88 opened this issue 4 years ago • 1 comments

New Issue Checklist

Describe the bug

Disabling shorthand_operator in source code for this line when defining an operator defining the shorthand operator, causes superfluous_disable_command warning. This only affects /= and *=; += and -= are not affected

Complete output when running SwiftLint, including the stack trace and command used
$ ./Pods/SwiftLint/swiftlint lint
...
Utils/Currency.swift:92:1: warning: Superfluous Disable Command Violation: SwiftLint rule 'shorthand_operator' did not trigger a violation in the disabled region. Please remove the disable command. (superfluous_disable_command)
Utils/Currency.swift:98:1: warning: Superfluous Disable Command Violation: SwiftLint rule 'shorthand_operator' did not trigger a violation in the disabled region. Please remove the disable command. (superfluous_disable_command)
Utils/Currency.swift:132:1: warning: Superfluous Disable Command Violation: SwiftLint rule 'shorthand_operator' did not trigger a violation in the disabled region. Please remove the disable command. (superfluous_disable_command)
Utils/Currency.swift:138:1: warning: Superfluous Disable Command Violation: SwiftLint rule 'shorthand_operator' did not trigger a violation in the disabled region. Please remove the disable command. (superfluous_disable_command)
...

Environment

  • SwiftLint version: 0.43
  • Installation method used: Cocoapods
  • Paste your configuration file:
included:
  - App
excluded:
  - "App/Libs/"
  - "App/Generated/"
  - "Pods/"
  - "Danger/"
opt_in_rules:
  - closure_body_length
  - closure_end_indentation
  - closure_spacing
  - collection_alignment
  - convenience_type
  - discouraged_object_literal
  - file_name_no_space
  - force_unwrapping
  - implicit_return
  - legacy_multiple
  - modifier_order
  - multiline_arguments
  - multiline_arguments_brackets
  - multiline_function_chains
  - multiline_literal_brackets
  - multiline_parameters
  - multiline_parameters_brackets
  - operator_usage_whitespace
  - redundant_type_annotation
  - sorted_imports
  - toggle_bool
  - unavailable_function
  - unneeded_parentheses_in_closure_argument
  - unused_declaration
  - unused_import
  - vertical_whitespace_between_cases
  - vertical_whitespace_closing_braces

# Rules configuration
cyclomatic_complexity:
  ignores_case_statements: true
deployment_target:
  iOS_deployment_target: 10.2
identifier_name:
  min_length: 2
force_cast: error
force_unwrapping: error
modifier_order:
  preferred_modifier_order: [final, acl, setterACL, override, dynamic, mutators, lazy, required, convenience, typeMethods, owned]
nesting:
  type_level: 2
type_name:
  min_length: 2

custom_rules:
  stop_ship:
    name: "stop_ship"
    regex: "(?i)STOPSHIP"
    match_kinds: comment
    message: "STOPSHIP must be fixed prior to release"
    severity: warning
  • Are you using nested configurations? If so, paste their relative paths and respective contents.
  • Which Xcode version are you using (check xcodebuild -version)? 12.4
  • 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.
struct Currency {
	private let value: Decimal

	init(value: Decimal) {
		self.value = value
	}

    @discardableResult
    public static func += (lhs: inout Currency, rhs: Currency) -> Currency {
        lhs = lhs + rhs // swiftlint:disable:this shorthand_operator <- No warning here
        return lhs
    }

    @discardableResult
    public static func -= (lhs: inout Currency, rhs: Currency) -> Currency {
        lhs = lhs - rhs // swiftlint:disable:this shorthand_operator <- No warning here
        return lhs
    }

    @discardableResult
    public static func /= (lhs: inout Currency, rhs: Currency) -> Currency {
        lhs = lhs / rhs // swiftlint:disable:this shorthand_operator <- Warning here
        return lhs
    }

    @discardableResult
    public static func *= (lhs: inout Currency, rhs: Currency) -> Currency {
        lhs = lhs * rhs // swiftlint:disable:this shorthand_operator <- Warning here
        return lhs
    }

    public static func + (lhs: Currency, rhs: Currency) -> Currency {
        Currency(value: lhs.value + rhs.value)
    }

    public static func - (lhs: Currency, rhs: Currency) -> Currency {
        Currency(value: lhs.value - rhs.value)
    }

    public static func / (lhs: Currency, rhs: Currency) -> Currency {
        Currency(value: lhs.value / rhs.value)
    }

    public static func * (lhs: Currency, rhs: Currency) -> Currency {
        Currency(value: lhs.value * rhs.value)
    }
}

mgray88 avatar Mar 11 '21 20:03 mgray88

I cannot reproduce the issue with the provided example and configuration. It perhaps has meanwhile been fixed. Can you confirm?

SimplyDanny avatar Sep 18 '22 14:09 SimplyDanny

Any update on this issue, @mgray88?

SimplyDanny avatar Jul 30 '23 15:07 SimplyDanny

We moved away from that particular struct so I'm not sure if it't still an issue. I'll see if I can reproduce it though

mgray88 avatar Jul 31 '23 12:07 mgray88

@SimplyDanny It looks like it's fixed! 🙌🏻

The examples on this page should probably be shown as static though, no?

public (static) func *= (lhs: inout Foo, rhs: Int) {
    lhs = lhs * rhs
}

mgray88 avatar Jul 31 '23 12:07 mgray88

The examples on this page should probably be shown as static though, no?

In freestanding operator declarations, static is not allowed. If they were part of a type declaration, static would be required.

SimplyDanny avatar Aug 01 '23 20:08 SimplyDanny