realm / realm/SwiftLint

`no_magic_numbers` flags the color components of `Color` and `NSColor`

Open
#6,869 0 comments 0 reactions 0 assignees View on GitHub

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
Bug Description

no_magic_numbers exempts the color components of a UIColor initializer, but the check matches that one type name only. SwiftUI's Color and AppKit's NSColor get no exemption, so the same values are reported there.

// No violation, as expected:
let uiKit = UIColor(red: 0.1, green: 0.42, blue: 0.7, alpha: 0.5)

// Three violations:
let swiftUI = Color(red: 0.1, green: 0.42, blue: 0.7)

// Three violations:
let appKit = NSColor(red: 0.1, green: 0.42, blue: 0.7, alpha: 0.5)

SwiftUI spells the opacity component opacity rather than alpha, so that label is not exempt either.

There is a second symptom in AppKit. NSColor names the first component after the color space, so even once the type name is recognised the first component is still reported while the rest are not:

// One violation on 0.1 only:
let color = NSColor(srgbRed: 0.1, green: 0.42, blue: 0.7, alpha: 1)

The same applies to calibratedRed, deviceRed, calibratedWhite, deviceWhite, genericGamma22White, calibratedHue, deviceHue, and to the CMYK components of NSColor(deviceCyan:magenta:yellow:black:alpha:).

$ swiftlint lint --no-cache
Environment
  • SwiftLint version 0.65.0
  • Xcode 26.5
  • Installation method used: building from source
  • Configuration file:
opt_in_rules:
  - no_magic_numbers

No nested configurations.

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 with SwiftLint's no_magic_numbers rule and reproduce the examples using swiftlint lint --no-cache. Check the reported violations for SwiftUI Color and AppKit NSColor, including the listed component-label variants. Done means the color component values in those initializers are exempt consistently with UIColor.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.