Redundant @objc Attribute

ID

swift.redundant_objc_attribute

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Swift

Tags

code-smell, redundancy

Description

Reports a declaration carrying @objc together with another attribute or modifier that already implies it. The bare @objc adds nothing but clutter.

The Swift compiler implies @objc for declarations carrying any of: @IBAction, @IBOutlet, @IBSegueAction, @IBInspectable, @IBDesignable, @NSManaged, @GKInspectable; for members of an @objcMembers class; and for declarations marked dynamic.

@objc @IBOutlet var label: UILabel!     // FLAW
@objc dynamic var x: Int = 0            // FLAW
@objc var y: Int = 0                    // OK — pure @objc
@objc(myName) func tapped() { }         // OK — explicit Obj-C name

Rationale

Redundant attributes hide intent: a reader has to know the implication rules to realise the second attribute does nothing. Keeping declarations minimal makes the relevant attributes more visible.

The @objc(name) form with an argument is explicitly renaming the Objective-C symbol and is never flagged.

Remediation

Drop the bare @objc:

@IBOutlet var label: UILabel!
dynamic var x: Int = 0