Rule Request: [no_number_literal_calculation]
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
New rule request
I run into code like the below a few times
let secondsInOneDay = 60 * 60 * 24
Since this kind of code can slow the type inference, the calculation should be done beforehand and only the answer should be used.
let secondsInOneDay = 86400
This rule should calculate the expression and replace it when running swiftlint fix.
Note
Since (I believe) this rule would be a subset of no_magic_numbers, it might be better if this was a configuration of no_magic_numbers rather than a separate rule.
- Why should this rule be added?
This can help users detect type inference-heavy codes and rewrite them easily.
- Provide several examples of what would and wouldn't trigger violations.
Triggering:
let secondsInOneDay = 60 * 60 * 24
let n = 4
let secondsInNDays = 60 * 60 * 24 * n
Non triggering:
let secondsInOneDay = 86400
let n = 4
let secondsInNDays = 86400 * n
- Should the rule be configurable, if so what parameters should be configurable?
No - Should the rule be opt-in or enabled by default? Why?
Personally I would love to have this rule by default but I think some people prefer60 * 60 * 24as their choice of coding style so this rule should be opt-in.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the existing no_magic_numbers rule and how swiftlint fix handles rule violations. Clarify whether this should be a separate opt-in rule or a no_magic_numbers configuration, then define completion around folding constant arithmetic such as 60 * 60 * 24 while leaving expressions involving variables unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100