为什么代码分析警告“使用逻辑 &&”当按位 &可能是故意的”被抚养?

发布于 2025-01-17 02:31:59 字数 1803 浏览 2 评论 0原文

代码:

BOOL CCreateReportDlg::CanSwapBrothers()
{
    BOOL            b1in2 = FALSE, b2in1 = FALSE;
    CStringArray    aryStrNames;

    // Must have valid data
    if (!IsSwapBrotherInit())
        return FALSE;

    // Get cell pointers
    auto pCell1 = GetSwapBrotherCell(1);
    auto pCell2 = GetSwapBrotherCell(2);
    if (pCell1 != nullptr && pCell2 != nullptr)
    {
        // Look for brother (cell 1) in cell 2 array
        auto strName = pCell1->GetText();
        pCell2->GetOptions(aryStrNames);
        const auto iNumNames = aryStrNames.GetSize();
        for (auto iName = 0; iName < iNumNames; iName++)
        {
            if (aryStrNames[iName] == strName)
            {
                b1in2 = TRUE;
                break;
            }
        }

        if (b1in2)
        {
            // Look for brother (cell 2) in cell 1 array
            auto strName = pCell2->GetText();
            pCell1->GetOptions(aryStrNames);
            const auto iNumNames = aryStrNames.GetSize();
            for (auto iName = 0; iName < iNumNames; iName++)
            {
                if (aryStrNames[iName] == strName)
                {
                    b2in1 = TRUE;
                    break;
                }
            }
        }
    }

    return b1in2 && b2in1;
}

感兴趣的行是 return 语句:

return b1in2 && b2in1;

我收到代码分析警告:

lnt 逻辑按位不匹配 当可能需要按位 & 时,使用逻辑 &&

就我而言,我的代码是正确的。为什么会提出这个问题?

Code:

BOOL CCreateReportDlg::CanSwapBrothers()
{
    BOOL            b1in2 = FALSE, b2in1 = FALSE;
    CStringArray    aryStrNames;

    // Must have valid data
    if (!IsSwapBrotherInit())
        return FALSE;

    // Get cell pointers
    auto pCell1 = GetSwapBrotherCell(1);
    auto pCell2 = GetSwapBrotherCell(2);
    if (pCell1 != nullptr && pCell2 != nullptr)
    {
        // Look for brother (cell 1) in cell 2 array
        auto strName = pCell1->GetText();
        pCell2->GetOptions(aryStrNames);
        const auto iNumNames = aryStrNames.GetSize();
        for (auto iName = 0; iName < iNumNames; iName++)
        {
            if (aryStrNames[iName] == strName)
            {
                b1in2 = TRUE;
                break;
            }
        }

        if (b1in2)
        {
            // Look for brother (cell 2) in cell 1 array
            auto strName = pCell2->GetText();
            pCell1->GetOptions(aryStrNames);
            const auto iNumNames = aryStrNames.GetSize();
            for (auto iName = 0; iName < iNumNames; iName++)
            {
                if (aryStrNames[iName] == strName)
                {
                    b2in1 = TRUE;
                    break;
                }
            }
        }
    }

    return b1in2 && b2in1;
}

The line of interest is the return statement:

return b1in2 && b2in1;

I am getting a code analysis warning:

lnt-logical-bitwise-mismatch
Using logical && when bitwise & was probably intended.

As far as I am concerned my code is correct. Why is this being raised?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间你老了 2025-01-24 02:32:00

编译器认为 && 适用于整数操作数以及将结果隐式转换为整数类型。 BOOL 有多个位;它与内置类型 bool 不同。

正如您链接到的页面中所述,“逻辑运算符与整数值一起使用”将导致此警告,并且此处肯定存在该情况。

“MFC”编码风格在很多方面违反了现代建议,使用非标准布尔类型只是较小的问题之一。 CStringArray 也是一种代码味道,现代 C++ 使用模板化容器并具有强大的算法来操作它们,您永远不应该自己编写搜索代码。

The compiler sees && applies to integer operands and an implicit conversion of the result to an integer type. BOOL has multiple bits; it isn't the same as the built-in type bool.

As noted in the page you linked to, "A logical operator was used with integer values" will cause this warning and that condition is certainly present here.

"MFC" coding styles violate modern recommendations in a lot of ways, using a non-standard boolean type is just one of the smaller issues. CStringArray is also a code smell, modern C++ uses templated containers and has powerful algorithms for manipulating them, you never should be writing search code yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文