Skip to content

Don't flag typed factory constants in no_magic_numbers - #6842

Closed
lechuckcaptain wants to merge 2 commits into
realm:mainfrom
lechuckcaptain:no-magic-numbers-typed-factory-constants
Closed

Don't flag typed factory constants in no_magic_numbers#6842
lechuckcaptain wants to merge 2 commits into
realm:mainfrom
lechuckcaptain:no-magic-numbers-typed-factory-constants

Conversation

@lechuckcaptain

Copy link
Copy Markdown

Note

Stacked on #6841. This branch is based on that one, so the diff below includes its commit too —
the second commit is the change described here. Happy to rebase onto main if you would rather
review them independently, or if #6841 is not wanted.

Summary

  • no_magic_numbers exempts a literal bound directly to a name, so let x: Int = 5 is clean while
    let x: Duration = .seconds(5) — the same constant, better typed — is flagged: the factory call
    breaks the grandparent chain isMagicNumber looks for. The rule effectively penalises the
    stronger API.
  • A literal is now exempt when it is the sole argument of a factory call forming the whole
    initializer of a declaration whose declared type is listed in the new definitional_types
    option, defaulting to Duration and Angle.
  • The type is read from the syntax — a member-access base (Duration.seconds(5)) or the
    declaration's type annotation (let x: Duration = .seconds(5)) — since the rule has no type
    information. That is the trade-off test_parent_classes already makes for superclass names; a
    typealias is not followed.

Gating on the declared type rather than on the call shape is what keeps arbitrary single-argument
factories flagged: in let x: Int = .factorial(20) and let d: Data = .randomBytes(64) the name
labels the result of a computation, not a unit. Requiring the call to be the initializer value
keeps use sites such as clock.sleep(for: .seconds(30)) flagged.

Measurement is deliberately absent from the default. Every construction is either
.init(value:unit:) or Measurement(value:unit:), excluded by the init check and the
sole-argument requirement respectively, so listing it would be a no-op.

Validation

  • swift test --filter NoMagicNumbersRuleGeneratedTests passes. No existing example was edited,
    reordered or removed; the new option is covered by configuration: examples.
  • swift run swiftlint-dev rules register run, so default_rule_configurations.yml is up to date.
  • swift run swiftlint clean on the changed files.
  • Sweep of a real 341-file package tree against a build of the base commit: 141 → 137 violations,
    4 removed, 0 introduced. The four are three Duration constants and one Angle.degrees(90).

Note for adopters: because this pattern is usually silenced with a disable comment today, projects
on --strict may see superfluous_disable_command fire after upgrading where a block existed only
for this. That is the intended payoff, but the upgrade is not silent.

@SwiftLintBot

SwiftLintBot commented Jul 28, 2026

Copy link
Copy Markdown
888 Messages
📖 Building this branch resulted in a binary size of 28460.32 KiB vs 28425.49 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.65 s vs 0.64 s on main (1% slower).
📖 Linting Alamofire with this PR took 0.93 s vs 0.94 s on main (1% faster).
📖 Linting Brave with this PR took 5.97 s vs 6.01 s on main (0% faster).
📖 Linting DuckDuckGo with this PR took 26.81 s vs 27.05 s on main (0% faster).
📖 Linting Firefox with this PR took 10.64 s vs 10.8 s on main (1% faster).
📖 Linting Kickstarter with this PR took 7.03 s vs 7.22 s on main (2% faster).
📖 Linting Moya with this PR took 0.41 s vs 0.37 s on main (10% slower).
📖 Linting NetNewsWire with this PR took 2.41 s vs 2.41 s on main (0% slower).
📖 Linting Nimble with this PR took 0.57 s vs 0.56 s on main (1% slower).
📖 Linting PocketCasts with this PR took 6.95 s vs 6.97 s on main (0% faster).
📖 Linting Quick with this PR took 0.33 s vs 0.38 s on main (13% faster).
📖 Linting Realm with this PR took 2.83 s vs 2.84 s on main (0% faster).
📖 Linting Sourcery with this PR took 1.63 s vs 1.66 s on main (1% faster).
📖 Linting Swift with this PR took 4.24 s vs 4.28 s on main (0% faster).
📖 Linting SwiftLintPerformanceTests with this PR took 0.16 s vs 0.16 s on main (0% slower).
📖 Linting VLC with this PR took 1.25 s vs 1.25 s on main (0% slower).
📖 Linting Wire with this PR took 15.36 s vs 15.35 s on main (0% slower).
📖 Linting WordPress with this PR took 9.68 s vs 9.68 s on main (0% slower).
📖 This PR fixed a violation in Aerial: /Aerial/Source/Views/PrefPanel/DisplayView.swift:62:51: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Aerial: /Aerial/Source/Views/PrefPanel/DisplayView.swift:63:47: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Aerial: /Aerial/Source/Views/PrefPanel/DisplayView.swift:69:51: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Aerial: /Aerial/Source/Views/PrefPanel/DisplayView.swift:70:47: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Aerial: /Aerial/Source/Views/PrefPanel/DisplayView.swift:166:47: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/App/BraveWidgets/LockScreenFavoriteWidget.swift:128:26: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Crypto/TransactionConfirmations/SaferSignTransactionView.swift:187:41: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Crypto/Accounts/AccountsView.swift:303:30: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Crypto/Onboarding/OnboardingNetworkSelectionView.swift:393:32: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:28:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:28:19: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:29:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:29:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:30:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:30:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:35:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:35:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:36:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:36:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:37:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:37:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:48:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:48:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:49:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:49:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:50:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:50:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:55:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:55:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:56:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:56:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:57:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:57:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:68:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:68:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:69:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:69:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:70:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:70:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:75:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:75:19: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:76:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:76:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:77:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:77:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:88:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:88:19: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:89:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:89:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:90:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:90:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:95:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:95:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:96:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:96:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:97:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:97:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:110:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:110:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:111:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:111:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:112:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:112:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:117:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:117:19: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:118:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:118:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:119:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:119:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:129:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:129:19: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:130:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:130:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:131:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:131:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:136:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:136:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:137:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:137:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:138:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:138:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:151:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:151:19: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:152:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:152:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:153:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:153:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:164:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:164:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:165:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:165:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:166:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:166:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:171:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:171:19: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:172:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:172:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:173:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:173:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:182:10: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:182:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:183:12: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:183:17: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:184:11: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:184:17: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:193:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:193:19: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:194:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:194:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:195:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:195:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:200:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:200:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:201:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:201:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:202:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:202:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:213:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:213:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:214:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:214:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:215:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:215:21: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:220:14: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:220:20: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:221:16: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:221:22: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
📖 This PR fixed a violation in Brave: /ios/brave-ios/Sources/BraveWallet/Extensions/WalletColors.swift:222:15: Warning: Magic numbers should be replaced by named constants (no_magic_numbers)
⚠️ Danger found 888 violations with this PR. Due to GitHub's max issue comment size, the number shown has been truncated to 147.

Generated by 🚫 Danger

`no_magic_numbers` already exempts the component arguments of a `UIColor`
initializer, but the check hardcoded that single type name, so SwiftUI's `Color`
and AppKit's `NSColor` got no exemption at all. Recognise all three, and accept
the `opacity` label alongside `alpha`, since SwiftUI spells it differently.

AppKit encodes the color space in the label of the first component, so cover the
`calibratedRed`, `deviceRed`, `srgbRed`, `calibratedWhite`, `deviceWhite`,
`genericGamma22White`, `calibratedHue` and `deviceHue` variants as well as the
`deviceCyan:magenta:yellow:black:alpha:` components. Recognising the type name
alone would have left the first component of such a call reported while the rest
were not.

The exemption also required the literal to be the *immediate* argument, so
`UIColor(red: 0.6, ...)` was exempt while `UIColor(red: 0x19 / 255, ...)` was
not: there the literal's parent is the division, not the labelled argument. Walk
up through arithmetic, parentheses and numeric conversions to find the enclosing
argument instead, and bail out on anything else so an exemption granted for one
argument cannot leak past it.

That boundary is what keeps the existing manual bit-packing example
`Int(r * 255) << 16` flagged, and `Color(...).opacity(0.42)` too, since in
neither case is the literal inside a color initializer's argument list.

Every literal remains judged by its own label, so an unrecognised label is
exempted for nothing but itself: `UIColor(rgb: 0x33373A, alpha: 0.16)` keeps
reporting the `rgb:` value while exempting `alpha:`, which is a color component
whatever its sibling happens to be.
The rule exempts a literal bound directly to a name, so `let x: Int = 5` is
clean while `let x: Duration = .seconds(5)` — the same constant, better typed —
is flagged: the factory call breaks the grandparent chain `isMagicNumber` looks
for. The rule therefore penalises the stronger API, and the finding tends to get
silenced with a disable comment rather than fixed.

Exempt a literal that is the sole argument of a factory call forming the whole
initializer of a declaration whose declared type is listed in the new
`definitional_types` option, defaulting to `Duration` and `Angle`. The type is
read from the syntax — a member-access base, or the declaration's type annotation
— because the rule has no type information; that is the same trade-off
`test_parent_classes` already makes when it matches written superclass names.

Gating on the declared type rather than on the call shape is what keeps arbitrary
single-argument factories flagged: in `let x: Int = .factorial(20)` and
`let d: Data = .randomBytes(64)` the name labels the result of a computation, not
a unit. Requiring the call to *be* the initializer value keeps use sites such as
`clock.sleep(for: .seconds(30))` flagged.
@lechuckcaptain
lechuckcaptain force-pushed the no-magic-numbers-typed-factory-constants branch from 5e6b547 to 352bbb1 Compare August 4, 2026 16:05
@lechuckcaptain

Copy link
Copy Markdown
Author

Closing for now. This was stacked on #6841, which I have closed in favour of a smaller series, so the diff here was misleading.

The change itself is still worth doing. I will raise it as a proposal issue first to agree the option name and default before reopening a PR, rather than leaving a draft sitting here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants