-
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
skip tuples for no_magic_numbers
rule
#5327
skip tuples for no_magic_numbers
rule
#5327
Conversation
Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoMagicNumbersRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoMagicNumbersRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoMagicNumbersRule.swift
Outdated
Show resolved
Hide resolved
Source/SwiftLintBuiltInRules/Rules/Idiomatic/NoMagicNumbersRule.swift
Outdated
Show resolved
Hide resolved
cb70268
to
101d4da
Compare
Looking at the OSS findings, there's another case I haven't thought of before. If there are more complicated expression in a tuple initialization like in |
So I thought the OSS violations we ended up with looked ok, but Maybe drop back to just allowing
That would still allow
or we could go further and require that the tuple elements all be simple expressions. |
I think that's the way to go. The rule currently triggers on |
Sounds good to me - |
Just to be sure: Also |
a12ad34
to
9f2d668
Compare
ok - so I think I have this working. As long as every element of the RHS tuple is an Integer or Float, we will allow it through, so
|
Example("let a = b / 100.0") | ||
Example("let a = b / 100.0"), | ||
Example("let (lowerBound, upperBound) = (400, 599)"), | ||
Example("let a = (5, 10)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also have a test for labeled tuple elements and a tuple used somewhere else that triggers the rule.
After that, I promise to accept the change. 😇
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I added those examples, and also extended the allowed tuple elements to include string literals, as cases like let notFound = (404, "Not Found")
seemed reasonable to skip as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But hold off on merging for right now @SimplyDanny - I want to checkout the OSS results, which now seem to have suspiciously lower fixes compared to before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok - that looks better. Should be good to go now 🤞 once CI passes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The strings and booleans make me think again. We now have:
let a = (3, 4) // ok
let a = (3, "a") // ok
let a = (3, b) // triggers on 3
let a = (3, (4, 5)) // triggers on all numbers
let a = (true, 4) // ok
let a = (3, 4 + 5) // triggers on all numbers
Does this make sense? Is it consistent and logical? 🤔 We might need to come up with a better set of rules for tuples I'm afraid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I totally get your point, but actually, all of the above examples would have been violations before, and there were enough reasonably allowable fixed violations in the OSS tests, that I think the change will add some value for some users.
let a = (3, b) // triggers on 3
let a = (3, (4, 5)) // triggers on all numbers
let a = (3, 4 + 5) // triggers on all numbers
will still be violations, but in the OSS code, there were few or none of these.
Users of the above examples would not be put at any disadvantage by this change - they would just need to disable the rule, like they would today.
I could definitely see a case for allowing let a = (3, (4, 5))
, but if someone wants that to pass, they can always request that we extend the coverage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. Let's have it merged then. Good job!
e08d78f
to
3d21880
Compare
Now ignores magic numbers in tuple assignments like
let (httpStatusCodeErrorLowerBound, httpStatusCodeErrorUpperBound) = (400, 599)
OSS checks look good to me.
Resolves #5305 by skipping magic numbers in tuple expressions altogether.