No Magic Numbers
Magic numbers should be replaced by named constants
- Identifier:
no_magic_numbers
- Enabled by default: No
- Supports autocorrection: No
- Kind: idiomatic
- Analyzer rule: No
- Minimum Swift compiler version: 5.0.0
- Default configuration:
Key Value severity warning test_parent_classes [“QuickSpec”, “XCTestCase”]
Non Triggering Examples
var foo = 123
static let bar: Double = 0.123
let a = b + 1.0
array[0] + array[1]
let foo = 1_000.000_01
// array[1337]
baz("9999")
func foo() {
let x: Int = 2
let y = 3
let vector = [x, y, -1]
}
class A {
var foo: Double = 132
static let bar: Double = 0.98
}
@available(iOS 13, *)
func version() {
if #available(iOS 13, OSX 10.10, *) {
return
}
}
enum Example: Int {
case positive = 2
case negative = -2
}
class FooTests: XCTestCase {
let array: [Int] = []
let bar = array[42]
}
class FooTests: XCTestCase {
class Bar {
let array: [Int] = []
let bar = array[42]
}
}
class MyTest: XCTestCase {}
extension MyTest {
let a = Int(3)
}
extension MyTest {
let a = Int(3)
}
class MyTest: XCTestCase {}
let foo = 1 << 2
let foo = 1 >> 2
let foo = 2 >> 2
let foo = 2 << 2
let a = b / 100.0
let range = 2 ..< 12
let range = ...12
let range = 12...
let (lowerBound, upperBound) = (400, 599)
let a = (5, 10)
let notFound = (statusCode: 404, description: "Not Found", isError: true)
#Preview {
ContentView(value: 5)
}
Triggering Examples
foo(↓321)
bar(↓1_000.005_01)
array[↓42]
let box = array[↓12 + ↓14]
let a = b + ↓2.0
let range = 2 ... ↓12 + 1
let range = ↓2*↓6...
let slice = array[↓2...↓4]
for i in ↓3 ..< ↓8 {}
let n: Int = Int(r * ↓255) << ↓16 | Int(g * ↓255) << ↓8
Color.primary.opacity(isAnimate ? ↓0.1 : ↓1.5)
class MyTest: XCTestCase {}
extension NSObject {
let a = Int(↓3)
}
if (fileSize > ↓1000000) {
return
}
let imageHeight = (width - ↓24)
return (↓5, ↓10, ↓15)
#ExampleMacro {
ContentView(value: ↓5)
}