Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@statementreply
Copy link
Contributor

@statementreply statementreply commented Jun 24, 2023

Works towards #3754. Works towards #2094.

This PR adds [[msvc::lifetimebound]] to std::minmax and std::ranges::minmax, which are more error-prone than std::min/std::max but not as widely used.

#define _SILENCE_LIFETIMEBOUND_WARNING to suppress the warning.

C:\Users\He\source\repos\STL\out>type test.cpp
#include <algorithm>

int square(int x) {
    return x * x;
}

int square_diff(int x, int y) {
    const auto [min, max] = std::minmax(square(x), square(y));
    return max - min;
}
C:\Users\He\source\repos\STL\out>clang -Wall -Wextra -std=c++17 -c test.cpp
test.cpp:8:41: warning: temporary whose address is used as value of local variable '[min, max]' will be destroyed at the
      end of the full-expression [-Wdangling]
    const auto [min, max] = std::minmax(square(x), square(y));
                                        ^~~~~~~~~
test.cpp:8:52: warning: temporary whose address is used as value of local variable '[min, max]' will be destroyed at the
      end of the full-expression [-Wdangling]
    const auto [min, max] = std::minmax(square(x), square(y));
                                                   ^~~~~~~~~
2 warnings generated.

C:\Users\He\source\repos\STL\out>set Esp.Extensions=CppCoreCheck.dll

C:\Users\He\source\repos\STL\out>cl /EHsc /W4 /std:c++17 /c /analyze:plugin EspXEngine.dll /analyze:ruleset ..\tests\std\rulesets\stl.ruleset test.cpp
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.37.32705 版
版权所有(C) Microsoft Corporation。保留所有权利。

test.cpp
C:\Users\He\source\repos\STL\out\test.cpp(8) : warning C26815: 指针无关联,因为它指向已销毁的临时实例。

@statementreply statementreply requested a review from a team as a code owner June 24, 2023 11:36
@strega-nil-ms
Copy link
Contributor

I really like this personally; however, I want to discuss it in the weekly meeting just to make sure we all agree 😄

@strega-nil-ms strega-nil-ms added enhancement Something can be improved decision needed We need to choose something before working on this labels Jun 27, 2023
@StephanTLavavej StephanTLavavej self-assigned this Jul 4, 2023
@StephanTLavavej StephanTLavavej removed the decision needed We need to choose something before working on this label Jul 5, 2023
@StephanTLavavej
Copy link
Member

We talked about this at the weekly maintainer meeting and are in favor of this approach. 😸

@StephanTLavavej StephanTLavavej removed their assignment Jul 7, 2023
@statementreply
Copy link
Contributor Author

@StephanTLavavej @strega-nil-ms I pushed changes after you approved.

@StephanTLavavej StephanTLavavej self-assigned this Jul 13, 2023
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej
Copy link
Member

I've had to push an additional commit to skip GH_002094_cpp_core_guidelines in the MSVC-internal test harness for now. We would need to modify the Perl test machinery to set up the Code Analysis ruleset; it is probably worth doing but can be done later.

FYI @strega-nil-ms as you previously approved.

@StephanTLavavej StephanTLavavej merged commit 7c7cc0c into microsoft:main Jul 14, 2023
@StephanTLavavej
Copy link
Member

Thanks for implementing these new diagnostics! ✅ ⚠️ 😻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Something can be improved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants