realm / realm/SwiftLint

Rule Request: [no_number_literal_calculation]

Open
#4,561 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

rule-request
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
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.


  1. Why should this rule be added?

This can help users detect type inference-heavy codes and rewrite them easily.

  1. 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
  1. Should the rule be configurable, if so what parameters should be configurable?
    No
  2. 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 prefer 60 * 60 * 24 as their choice of coding style so this rule should be opt-in.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.