☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

注释应出现在合理的位置

3.5.2 ID_badCommentPosition
目录 › next › previous

注释应出现在段落的前后或行尾,不应出现在行首或中间,否则干扰阅读,甚至会导致标准未定义的行为。

示例:

#/*comment*/include "foo.h"         // Non-compliant
#include <bar.h /*comment*/>        // Non-compliant, undefined behavior

/*comment*/ int main()              // Non-compliant
{
    return a + b /*comment*/ + c;   // Non-compliant
}

应改为:

#include "foo.h"    // comment      // Compliant
#include <bar.h>    // comment      // Compliant

/*
 * comment                          // Compliant
 */
int main()
{
    return a + b + c;  // comment   // Compliant
}

例外:

void foo(int i = 0);                // Declaration

void foo(int i /*= 0*/) {           // Let it go
    ....
}

如果参数有默认值,在函数实现中参数声明的结尾可用注释说明,不受本规则限制。

依据

ISO/IEC 9899:1999 6.4.7(3)-undefined ISO/IEC 9899:2011 6.4.7(3)-undefined ISO/IEC 14882:2003 2.8(2)-undefined ISO/IEC 14882:2011 2.9(2)-implementation

参考

CWE-1113
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.