UndetachedDelegate rule trigger by setter method that passes delegate to another object

Issue #47 closed
Former user created an issue

Let's say you have this class Structure

@interface ClassA

- (void)setClassBDelegate:(id<ClassBDelegate>)delegate;

@end

@implementation ClassA

- (void)setClassBDelegate:(id<ClassBDelegate>)delegate {
    self.classBInstance.delegate = delegate;
}

@end

@interface ClassB

@property (weak, nonatomic) id<ClassBDelegate> delegate;

@end

Calling [self.classAInstance setClassBDelegate:self] triggers the UndetachedDelegate rule, even though the delegate property being set is weak

Comments (6)

  1. Ben Asher

    I created this issue. I didn't realize Atlassian did all of the crazy stuff when you put an \@ in front of a word.

  2. Ben Asher

    Here's how this should look without all of the crazy markup I just did:

    @interface ClassA
    
    - (void)setClassBDelegate:(id<ClassBDelegate>)delegate;
    
    @end
    
    @implementation ClassA
    
    - (void)setClassBDelegate:(id<ClassBDelegate>)delegate {
        self.classBInstance.delegate = delegate;
    }
    
    @end
    
    @interface ClassB
    
    @property (weak, nonatomic) id<ClassBDelegate> delegate;
    
    @end
    
  3. Ali Rantakari repo owner

    Thanks for reporting this.

    The problem here is that Faux Pas considers -[ClassA setClassBDelegate:] a setter for a property (because it’s named using the naming convention for setters). However in this case the setter has no associated getter, which means that Faux Pas shouldn’t consider that method a property setter. This fix will be included in the next release.

    Even considering this upcoming fix, it’s also worth noting that this rule doesn’t try to inspect the implementation of properties’ setters — it just depends on the interface declaration. So if the method -[ClassA classBDelegate] existed, and Faux Pas would thus consider -[ClassA setClassBDelegate:] a property setter, it would again emit a warning if the setter and getter methods were declared as such (i.e. without the @property syntax) in the ClassA interface. If the @property (weak, …) id<ClassBDelegate> classBDelegate declaration was used instead, Faux Pas would be able to see that the property is weak and would thus suppress the warning.

  4. Log in to comment