[英]How to detect “potential loss of precision in explicit type conversion” in C++ code by using static analysis tool or compiler flag
For example:例如:
long long a = ....;
int b;
b = (int) a;
Is there a gcc flag or static analysis tools like Sonarqube can detect this case?是否有像 Sonarqube 这样的分析工具可以检测到这种情况?
Searching for explicit type castings is a strange task.搜索显式类型转换是一项奇怪的任务。 The developer obviously meant that he wanted to "cut off" high bits using explicit type casting.开发人员显然意味着他想使用显式类型转换“切断”高位。 Nevertheless, I would venture to say that the task was actually different: one had to port a 32-bit application to the 64-bit platform.不过,我敢说任务实际上是不同的:必须将 32 位应用程序移植到 64 位平台。 In this case, yes, it is a very relevant task to detect dangerous explicit types castings.在这种情况下,是的,检测危险的显式类型转换是一项非常相关的任务。 If it is so, there is a solution.如果是这样,有一个解决方案。 There is a special set of diagnostics related to porting in the PVS-Studio analyzer.在 PVS-Studio 分析器中有一组与移植相关的特殊诊断。 Examples:例子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.