NSError created with alloc/init instead of the class convenience method

ID

objectivec.correctness.avoid_nserror_init

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Api Misuse

Language

Objective-C

Description

Build errors with +[NSError errorWithDomain:code:userInfo:] rather than alloc/init. [[NSError alloc] init] in particular yields an error with an empty domain and code 0, which carries no information to the caller and cannot be matched against a domain constant; the convenience method makes the three required pieces explicit at the call site.

Rationale

Build errors with +[NSError errorWithDomain:code:userInfo:] rather than alloc/init. [[NSError alloc] init] in particular yields an error with an empty domain and code 0, which carries no information to the caller and cannot be matched against a domain constant; the convenience method makes the three required pieces explicit at the call site.

The following code illustrates the pattern detected by this rule:

@implementation ArticleLoader
- (NSError *)emptyFailure {
    // FLAGGED: NSError created with alloc/init instead of the class convenience method
    return [[NSError alloc] init];
}
@end

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved