-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Comments
heh |
I wonder if we should give up and just detect cases when the variable is the first operand 😅 |
@alistra your example is actually a different problem and should be fixed on |
This still seems to be an issue with 0.22. This code triggers the warning in the 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. |
@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 |
@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 |
You're defining The rule always assume that you can use 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? |
Ah. I think you mean since |
It's not the What we can do is check if a violation happens inside a shorthand operator definition (e.g. |
Oh, so the warning always expects you to be able to use |
Exactly that :) The rule shouldn't want you to use a shorthand operator on its own function implementation. |
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:The text was updated successfully, but these errors were encountered: