CodeQL 文档

声明隐藏变量

ID: cpp/declaration-hides-variable
Kind: problem
Security severity: 
Severity: recommendation
Precision: high
Tags:
   - maintainability
   - readability
Query suites:
   - cpp-security-and-quality.qls

单击查看 CodeQL 代码库中的查询

此规则查找在包围作用域中隐藏局部变量的局部变量声明。这些声明创建了具有相同名称但作用域不同的变量。这使得难以确定表达式中实际使用的是哪个变量。

建议

考虑更改其中一个变量的名称以使其与另一个变量区分开。

示例

void f() {
	int i = 10;

	for (int i = 0; i < 10; i++) { //the loop counter hides the variable
		 ...
	}

	{
		int i = 12; //this variable hides the variable in the outer block
		...
	}
}

参考

  • B. Stroustrup. The C++ Programming Language Special Edition p 82. Addison Wesley. 2000.

  • ©GitHub, Inc.
  • 条款
  • 隐私