【问题标题】:Error on missing return statement缺少退货声明时出错
【发布时间】:2011-03-30 01:20:42
【问题描述】:

如何在 GCC 下为缺少的 return 语句生成错误?

cpfsfuse.c:184: warning: no return statement in function returning non-void

我能够为隐式函数声明 (-Werror-implicit-function-declaration) 返回错误,并且我知道有一个 -Werror= 开关,但我找不到适当的警告来提升到错误状态。

我怎样才能做到这一点?

【问题讨论】:

    标签: gcc compiler-errors return


    【解决方案1】:

    您应该能够使用-fdiagnostics-show-option 选项来显示Werror= 开关的正确标志。摘自this 博文:

    % gcc -x c -Wall -Wextra -fdiagnostics-show-option -c -o /dev/null - <<EOF
    int foo() {
    }
    EOF
    <stdin> In function ‘foo’:
    <stdin>:2: warning: control reaches end of non-void function [-Wreturn-type]
    

    实际上,return-type 标志可能是您想要的。

    【讨论】:

    • 请注意,使用 -Wreturn-type 当缺少 return 语句时,您不会收到 main() 函数的任何警告。
    • 我有一个 GoogleTest 套件在 CI 作业中生成误报,因为 main() 中的默认返回。您还知道在 main() 中强制执行显式返回的任何标志吗?
    猜你喜欢
    • 2012-10-04
    • 2013-07-11
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2012-03-23
    • 1970-01-01
    相关资源
    最近更新 更多