在Visual C ++中是否有相当于gcc的-Wshadow

时间:2020-12-14 21:01:22

-Wshadow will "Warn whenever a local variable shadows another local variable.". Is there an equivalent in Visual C++ (2008)? I tried /W4 but it didn't pick up on it. I also tried Cppcheck but that didn't see it either.

-Wshadow将“当局部变量影响另一个局部变量时发出警告。” Visual C ++(2008)中是否存在等价物?我试过/ W4但它没有接受它。我也尝试过Cppcheck,但也没有看到它。

e.g. if I inadvertently do:

例如如果我无意中做了:

class A
{
        private:
                int memberVar;
        public:
                void fn()
                {
                        int memberVar = 27;
                }
};

I would really like to know about it!

我真的很想知道它!

5 个解决方案

#1


7  

I am afraid no.

我不敢。

You could perhaps try compiling your code with Clang:

您也许可以尝试使用Clang编译代码:

  • it has this warning (and a lot of others)
  • 它有这个警告(还有很多其他的)

  • it has a compatibility mode for MSVC headers (and can build most of MFC)
  • 它具有MSVC头的兼容模式(并且可以构建大多数MFC)

We use gcc at work, to build our code, but compile with Clang regularly to test the code conformance to the Standard and benefit from its warnings.

我们在工作中使用gcc来构建我们的代码,但是定期与Clang一起编译以测试代码与标准的一致性并从其警告中受益。

#2


8  

Check out warnings C6244 and C6246

查看警告C6244和C6246

But you will need to enable automatic code analysis to get them, see How to: Enable and Disable Automatic Code Analysis for C/C++

但是您需要启用自动代码分析才能获取它们,请参阅如何:启用和禁用C / C ++的自动代码分析

If you cannot do it in your VS edition (Analyzing Managed Code Quality by Using Code Analysis), try to add the /analyze flag to the compilation command line. You will get some warnings that the '/analyze-' flag, which has been added by your IDE, is replaced with the manually added '/analyze' flag, but the analysis will work ;-)

如果在VS版本中无法执行此操作(使用代码分析分析托管代码质量),请尝试将/ analyze标志添加到编译命令行。您将收到一些警告,表示IDE已添加的'/ analyze-'标志被替换为手动添加的'/ analyze'标志,但分析将起作用;-)

#3


4  

There's no warning about this that's disabled by default, so if you're not seeing the warning at the default levels, I'd say it can't be done. (Which is lame.)

没有任何警告,默认情况下禁用此功能,因此如果您没有看到默认级别的警告,我会说无法完成。 (这是蹩脚的。)

#4


4  

Visual Studio 2015 now warns about shadowed variables by default. Excerpt from http://blogs.msdn.com/b/vcblog/archive/2014/11/12/improvements-to-warnings-in-the-c-compiler.aspx follows:

Visual Studio 2015现在默认警告阴影变量。摘录自http://blogs.msdn.com/b/vcblog/archive/2014/11/12/improvements-to-warnings-in-the-c-compiler.aspx如下:

Shadowed variables A variable declaration "shadows" another if the enclosing scope already contains a variable with the same name. For example:

阴影变量如果封闭范围已包含具有相同名称的变量,则变量声明“阴影”另一个。例如:

void f(int x) 
{ 
    int y;
    { 
        char x; //C4457
        char y; //C4456
    } 
}

The inner declaration of x shadows the parameter of function f, so the compiler will emit: warning C4457: declaration of 'x' hides function parameter The inner declaration of y shadows the declaration of y in the function scope, so the compiler will emit: warning C4456: declaration of 'y' hides previous local declaration Note that, as before, a variable declaration with the same name as a function parameter but not enclosed in an inner scope triggers an error instead:

x的内部声明影响函数f的参数,因此编译器将发出:警告C4457:声明'x'隐藏函数参数y的内部声明在函数作用域中隐藏y的声明,因此编译器将发出:警告C4456:'y'的声明隐藏了以前的本地声明请注意,与之前一样,与函数参数同名但未包含在内部作用域中的变量声明会触发错误:

void f(int x) 
{ 
    char x; //C2082
}

The compiler emits: error C2082: redefinition of formal parameter 'x'

编译器发出:错误C2082:重新定义形式参数'x'

#5


1  

(I would add this as a comment to Dawn's answer, but have insufficient reputation at the moment)

(我会将此作为对Dawn答案的评论,但目前声誉不足)

There is an issue open on Microsoft Connect petitioning to have the warning upgraded from Code Analysis to standard compilation. I suggest upvoting it to try to get Microsoft's attention.

Microsoft Connect请求打开一个问题,将警告从代码分析升级到标准编译。我建议推荐它以试图引起微软的注意。

#1


7  

I am afraid no.

我不敢。

You could perhaps try compiling your code with Clang:

您也许可以尝试使用Clang编译代码:

  • it has this warning (and a lot of others)
  • 它有这个警告(还有很多其他的)

  • it has a compatibility mode for MSVC headers (and can build most of MFC)
  • 它具有MSVC头的兼容模式(并且可以构建大多数MFC)

We use gcc at work, to build our code, but compile with Clang regularly to test the code conformance to the Standard and benefit from its warnings.

我们在工作中使用gcc来构建我们的代码,但是定期与Clang一起编译以测试代码与标准的一致性并从其警告中受益。

#2


8  

Check out warnings C6244 and C6246

查看警告C6244和C6246

But you will need to enable automatic code analysis to get them, see How to: Enable and Disable Automatic Code Analysis for C/C++

但是您需要启用自动代码分析才能获取它们,请参阅如何:启用和禁用C / C ++的自动代码分析

If you cannot do it in your VS edition (Analyzing Managed Code Quality by Using Code Analysis), try to add the /analyze flag to the compilation command line. You will get some warnings that the '/analyze-' flag, which has been added by your IDE, is replaced with the manually added '/analyze' flag, but the analysis will work ;-)

如果在VS版本中无法执行此操作(使用代码分析分析托管代码质量),请尝试将/ analyze标志添加到编译命令行。您将收到一些警告,表示IDE已添加的'/ analyze-'标志被替换为手动添加的'/ analyze'标志,但分析将起作用;-)

#3


4  

There's no warning about this that's disabled by default, so if you're not seeing the warning at the default levels, I'd say it can't be done. (Which is lame.)

没有任何警告,默认情况下禁用此功能,因此如果您没有看到默认级别的警告,我会说无法完成。 (这是蹩脚的。)

#4


4  

Visual Studio 2015 now warns about shadowed variables by default. Excerpt from http://blogs.msdn.com/b/vcblog/archive/2014/11/12/improvements-to-warnings-in-the-c-compiler.aspx follows:

Visual Studio 2015现在默认警告阴影变量。摘录自http://blogs.msdn.com/b/vcblog/archive/2014/11/12/improvements-to-warnings-in-the-c-compiler.aspx如下:

Shadowed variables A variable declaration "shadows" another if the enclosing scope already contains a variable with the same name. For example:

阴影变量如果封闭范围已包含具有相同名称的变量,则变量声明“阴影”另一个。例如:

void f(int x) 
{ 
    int y;
    { 
        char x; //C4457
        char y; //C4456
    } 
}

The inner declaration of x shadows the parameter of function f, so the compiler will emit: warning C4457: declaration of 'x' hides function parameter The inner declaration of y shadows the declaration of y in the function scope, so the compiler will emit: warning C4456: declaration of 'y' hides previous local declaration Note that, as before, a variable declaration with the same name as a function parameter but not enclosed in an inner scope triggers an error instead:

x的内部声明影响函数f的参数,因此编译器将发出:警告C4457:声明'x'隐藏函数参数y的内部声明在函数作用域中隐藏y的声明,因此编译器将发出:警告C4456:'y'的声明隐藏了以前的本地声明请注意,与之前一样,与函数参数同名但未包含在内部作用域中的变量声明会触发错误:

void f(int x) 
{ 
    char x; //C2082
}

The compiler emits: error C2082: redefinition of formal parameter 'x'

编译器发出:错误C2082:重新定义形式参数'x'

#5


1  

(I would add this as a comment to Dawn's answer, but have insufficient reputation at the moment)

(我会将此作为对Dawn答案的评论,但目前声誉不足)

There is an issue open on Microsoft Connect petitioning to have the warning upgraded from Code Analysis to standard compilation. I suggest upvoting it to try to get Microsoft's attention.

Microsoft Connect请求打开一个问题,将警告从代码分析升级到标准编译。我建议推荐它以试图引起微软的注意。