【问题标题】:How to force “Expression result unused” warning to happen如何强制发生“未使用的表达式结果”警告
【发布时间】:2014-03-07 17:23:50
【问题描述】:

当我编译语句[[NSArray alloc] init]; 时,clang 给出“警告:表达式结果未使用 [-Wunused-value]”。

如何从我自己的函数中引发“未使用表达式结果”警告?例如:

@interface SimplePromise : NSObject
-(SimplePromise*)then:(id(^)(id result))block;
@end

-(void)someMethod {
    // I want this statement to cause a warning because
    // the transformed promise is dealloc’d before the block ever executes!
    [self.fetchPromise then:^id(id result) {
        self.fetchedData = result;
        return nil;
    }];
}

【问题讨论】:

  • 我相信它只会在它是一个 init 方法并且它没有被保存在变量中或被其他东西使用时才会抱怨。换句话说,你初始化了一些东西,但不做任何事情来保持对它的引用。它似乎并没有抱怨其他返回值的方法,而且我认为你不能做任何事情来让它只是简单地出现在你面前。但是,如果您想创建自己的警告,可以在代码中使用 #warning。
  • 在我看来你想要一个“未使用的返回值结果”的警告,对吧?
  • @Merlevede,是的,如果特定方法的返回值被丢弃,我想要一个警告。

标签: objective-c xcode clang


【解决方案1】:

用__attribute__((warn_unused_result))声明你的方法:

-(SimplePromise*)then:(id(^)(id result))block  __attribute__((warn_unused_result));

【讨论】:

    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    相关资源
    最近更新 更多