禁用单个警告错误

Coo*_*kie 102 c++ warnings pragma visual-c++

有没有办法用visual studio禁用cpp文件中的单个警告线?

例如,如果我捕获异常并且不处理它,则会收到错误4101(未引用的局部变量).有没有办法在该函数中忽略它,否则在编译单元中报告它?此刻,我放在#pragma warning (disable : 4101)文件的顶部,但显然只是将它关闭整个单元.

And*_*nck 165

#pragma warning( push )
#pragma warning( disable : 4101)
// Your function
#pragma warning( pop ) 
Run Code Online (Sandbox Code Playgroud)

  • `clang`似乎不支持这个pragma,但你可以通过`#pragma clang diagnostic push`,`#pragma clang diagnostic ignored"-Wunused-variable"`和`#pragma clang diagnostic pop`来达到同样的效果. .请参阅Clang用户手册中的"通过编类控制诊断"(http://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas) (4认同)
  • 我认为这也适用于include? (2认同)

Dan*_*her 80

如果您只想在一行代码中禁止警告,则可以使用suppress 警告说明符:

#pragma warning(suppress: 4101)
// here goes your single line of code where the warning occurs
Run Code Online (Sandbox Code Playgroud)

对于单行代码,这与编写以下代码相同:

#pragma warning(push)
#pragma warning(disable: 4101)
// here goes your code where the warning occurs
#pragma warning(pop)
Run Code Online (Sandbox Code Playgroud)

  • 很有用!不幸的是,它不适用于包含生成警告的标题的单行. (8认同)
  • @MarkoPopovic:“抑制”说明符在单个经过预处理的代码行上运行。如果#pragma warning(suppress:...)之后的行是#include指令(它将包含其参数的文件扩展到当前编译单元中),则该效果仅适用于该文件的第一行。这应该是显而易见的,因为警告是由编译器生成的。编译器对预处理的代码进行操作。 (2认同)
  • @voi:*“-ed” *结尾表示[过去分词](https://en.wikipedia.org/wiki/分词)。它用来表示过去已经结束了。*“已预处理” *行是已完全处理的行。 (2认同)

Mat*_*lia 28

#pragma push/pop通常是解决这类问题的方法,但在这种情况下你为什么不删除未引用的变量呢?

try
{
    // ...
}
catch(const your_exception_type &) // type specified but no variable declared
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

  • @Sjoerd作为未来的读者我证明这个答案实际上对我有帮助. (9认同)
  • 对于这个问题,这不是一个问题.当然,这可能会解决OP的问题,但无法帮助未来的读者提出类似的问题:"我如何为代码的特定部分关闭特定警告?" (6认同)
  • @Mołot:作为过去的作家,我很高兴它有所帮助。=) (2认同)

sha*_*oth 9

#pragma warning ( push )然后使用,然后输入#pragma warning ( disable )您的代码,然后#pragma warning ( pop )按照此处的描述使用:

#pragma warning( push )
#pragma warning( disable : WarningCode)
// code with warning
#pragma warning( pop ) 
Run Code Online (Sandbox Code Playgroud)


ori*_*zil 6

正如@rampion 提到的,如果您在 clang gcc 中,警告是按名称而不是编号,您需要执行以下操作:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
// ..your code..
#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)

这个信息来自这里


Gab*_*les 6

这个问题是Google 搜索“如何在 c++ 中抑制 -Wunused-result” 的前 3 个点击之一,所以我在这里添加这个答案,因为我找到了它并希望帮助下一个人。

如果您的警告/错误是-Wunused(或其子错误之一)或-Wunused -Werror ,解决方案是转换为void

对于仅1-Wunused或其子错误之一,您可以将其强制转换为以禁用警告。这应该适用于任何 C 和 C++ 编译器以及任何 IDE。void

1注 1:例如,请参阅此处的 gcc 文档,了解这些警告的列表:https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html,然后搜索短语“以上所有 -Wunused选项组合”,并在其中查找主要-Wunused警告,并在其上方查找子警告。包含的子警告-Wunused包括:

  • -Wunused-but-set-parameter
  • -Wunused-but-set-variable
  • -Wunused-function
  • -Wunused-label
  • -Wunused-local-typedefs
  • -Wunused-parameter
  • -Wno-unused-result
  • -Wunused-variable
  • -Wunused-const-variable
  • -Wunused-const-variable=n
  • -Wunused-value
  • -Wunused= 包含以上所有-Wunused选项的组合

强制转换 to 来抑制此警告的示例void

// some "unused" variable you want to keep around
int some_var = 7;
// turn off `-Wunused` compiler warning for this one variable
// by casting it to void
(void)some_var;  // <===== SOLUTION! ======
Run Code Online (Sandbox Code Playgroud)

对于 C++,这也适用于返回标有 的变量的函数[[nodiscard]]

C++ 属性:nodiscard (C++17 起)
如果从废弃值表达式(而不是强制转换为 void)调用声明为 nodiscard 的函数或按值返回声明为 nodiscard 的枚举或类的函数,则鼓励编译器发出警告。
(来源:https ://en.cppreference.com/w/cpp/language/attributes/nodiscard )

因此,解决方案是将函数调用强制转换为void,因为这实际上是将函数返回的值(用属性标记[[nodiscard]])强制转换为void

例子:

// Some class or struct marked with the C++ `[[nodiscard]]` attribute
class [[nodiscard]] MyNodiscardClass 
{
public:
    // fill in class details here
private:
    // fill in class details here
};

// Some function which returns a variable previously marked with
// with the C++ `[[nodiscard]]` attribute
MyNodiscardClass MyFunc()
{
    MyNodiscardClass myNodiscardClass;
    return myNodiscardClass;
}

int main(int argc, char *argv[])
{
    // THE COMPILER WILL COMPLAIN ABOUT THIS FUNCTION CALL
    // IF YOU HAVE `-Wunused` turned on, since you are 
    // discarding a "nodiscard" return type by calling this
    // function and not using its returned value!
    MyFunc();

    // This is ok, however, as casing the returned value to
    // `void` suppresses this `-Wunused` warning!
    (void)MyFunc();  // <===== SOLUTION! ======
}
Run Code Online (Sandbox Code Playgroud)

最后,您还可以使用 C++17[[maybe_unused]]属性:https://en.cppreference.com/w/cpp/language/attributes/maybe_unused


Chr*_*n.K 5

相反,把它放在文件(甚至是头文件)之上的,只是包装与问题的代码#pragma warning (push)#pragma warning (disable)以及匹配#pragma warning (pop),如图所示这里

尽管还有其他一些选择,包括#pramga warning (once)


Aja*_*jay 5

也可以使用中UNREFERENCED_PARAMETER定义的WinNT.H。定义是:

#define UNREFERENCED_PARAMETER(P)          (P)
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

void OnMessage(WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(wParam);
    UNREFERENCED_PARAMETER(lParam);
}
Run Code Online (Sandbox Code Playgroud)

为什么要使用它,您可能会争辩说您可以忽略变量名本身。好吧,在某些情况下(不同的项目配置,调试/发布版本),实际可能会使用该变量。在另一种配置中,该变量处于未使用状态(因此出现警告)。

某些静态代码分析可能仍会对此不切实际的声明(wParam;)发出警告。在这种情况下,您DBG_UNREFERENCED_PARAMETER可以使用与UNREFERENCED_PARAMETER调试版本相同的功能,而P=P在发行版本中使用相同的功能。

#define DBG_UNREFERENCED_PARAMETER(P)      (P) = (P)
Run Code Online (Sandbox Code Playgroud)


A87*_*876 5

例:

#pragma warning(suppress:0000)  // (suppress one error in the next line)
Run Code Online (Sandbox Code Playgroud)

此编译指示对于从Visual Studio 2005开始的C ++有效。https: //msdn.microsoft.com/zh-cn/library/2c8f766e( v= vs.80)
.aspx

该杂项对于从Visual Studio 2005到Visual Studio 2015的C#无效。
错误:“预期禁用或还原”。
(我想他们永远都无法实现suppress...)https://msdn.microsoft.com/zh-cn/library/441722ys(v=vs.140 )
.aspx

C#需要不同的格式。它看起来像这样(但不起作用):

#pragma warning suppress 0642  // (suppress one error in the next line)
Run Code Online (Sandbox Code Playgroud)

而不是suppress,您必须disableenable

if (condition)
#pragma warning disable 0642
    ;  // Empty statement HERE provokes Warning: "Possible mistaken empty statement" (CS0642)
#pragma warning restore 0642
else
Run Code Online (Sandbox Code Playgroud)

这太丑了,我认为重新设置样式会更聪明:

if (condition)
{
    // Do nothing (because blah blah blah).
}
else
Run Code Online (Sandbox Code Playgroud)