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

Shorthand Operator False Positive - String concatenation #1182

Closed
ghost opened this issue Jan 13, 2017 · 12 comments
Closed

Shorthand Operator False Positive - String concatenation #1182

ghost opened this issue Jan 13, 2017 · 12 comments
Labels
bug Unexpected and reproducible misbehavior.

Comments

@ghost
Copy link

ghost commented Jan 13, 2017

The + operator is commutative when working with numbers, but when used for string concatenation it is not. For example, the following code is a violation with the rules currently:

var helloWorld = "world!"
helloWorld = "Hello, " + helloWorld
@marcelofabri marcelofabri added the bug Unexpected and reproducible misbehavior. label Jan 13, 2017
@jpsim
Copy link
Collaborator

jpsim commented Jan 13, 2017

heh

@marcelofabri
Copy link
Collaborator

I wonder if we should give up and just detect cases when the variable is the first operand 😅

@alistra
Copy link

alistra commented Jan 16, 2017

messages image 3384656306

@marcelofabri
Copy link
Collaborator

@alistra your example is actually a different problem and should be fixed on master

@jshier
Copy link
Contributor

jshier commented Sep 8, 2017

This still seems to be an issue with 0.22. This code triggers the warning in the += implementation:

extension NSAttributedString {
    static func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSAttributedString {
        let result = NSMutableAttributedString()
        result.append(lhs)
        result.append(rhs)
        return result
    }

    static func += (lhs: inout NSAttributedString, rhs: NSAttributedString) {
        lhs = lhs + rhs // Shorthand Operator Violation
    }
}

Unless I'm mistaken, there isn't a better way to express this.

@marcelofabri
Copy link
Collaborator

@jshier That's a different issue: you can't use the operator because you're actually defining it on that function. Please file another issue for that.

However, since that's not a super common case, I'd suggest you to use swiftlint:disable to ignore the violation for now.

@jshier
Copy link
Contributor

jshier commented Sep 8, 2017

@marcelofabri Before I file an issue, I'd like to understand it. I'm not sure what "actually defining it on that function" means. The + for NSAttributedString is defined above the += implementation. Also, like +, += should already be a defined operator from the standard library.

@marcelofabri
Copy link
Collaborator

You're defining += for NSAttributedString. As so, you can't use += in its implementation.

The rule always assume that you can use += when + is used. Currently, SwiftLint has no way to find every operator declared on a project/module: the only information we have when linting a file is from that file itself.

In your case, it'd be possible to avoid triggering a violation if we'd trigger it, but we're inside the definition of the shorthand operator that'd be preferred. Does that make sense?

@jshier
Copy link
Contributor

jshier commented Sep 8, 2017

Ah. I think you mean since + is defined by me, I can't use it in += without triggering the warning, as the warning can't tell that + is custom too. What issue would you like for this? Detection of custom operator pairs?

@marcelofabri
Copy link
Collaborator

It's not the + the problem. The problem is that you're inside the += definition. If you use += (which the rule wants you to do), you'd get an infinite recursion.

What we can do is check if a violation happens inside a shorthand operator definition (e.g. +=) and if the violation is triggered on the same operation (e.g. +).

@jshier
Copy link
Contributor

jshier commented Sep 8, 2017

Oh, so the warning always expects you to be able to use +=, even inside a custom += definition. So what issue would you like me to file here?

@marcelofabri
Copy link
Collaborator

Exactly that :)

The rule shouldn't want you to use a shorthand operator on its own function implementation.

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