Finds placement-new calls where the pointer type of the adress mismatches the type of the created value
Details
Diff Detail
Event Timeline
clang-tidy/misc/PlacementNewTargetSizeCheck.cpp | ||
---|---|---|
10 ↗ | (On Diff #193318) | Unnecessary empty line. Please run Clang-format after fixing. |
23 ↗ | (On Diff #193318) | Please use static instead of anonymous namespaces. See LLVM Coding Guidelines. |
26 ↗ | (On Diff #193318) | Please don't use auto where type could not be easily deduced. |
28 ↗ | (On Diff #193318) | Please don't use auto where type could not be easily deduced. |
47 ↗ | (On Diff #193318) | auto could be used here, because of cast. |
50 ↗ | (On Diff #193318) | Please don't use auto where type could not be easily deduced. |
55 ↗ | (On Diff #193318) | Please don't use auto where type could not be easily deduced. |
62 ↗ | (On Diff #193318) | auto could be used here, because of cast. |
63 ↗ | (On Diff #193318) | Please don't use auto where type could not be easily deduced. |
65 ↗ | (On Diff #193318) | Please run Clang-format. |
73 ↗ | (On Diff #193318) | Unnecessary empty line. |
docs/ReleaseNotes.rst | ||
135 | Please add one sentence description. Should be same as in documentation. | |
docs/clang-tidy/checks/misc-placement-new-target-size.rst | ||
5 ↗ | (On Diff #193318) | Unnecessary empty line. |
docs/clang-tidy/checks/misc-placement-new-target-size.rst | ||
---|---|---|
5 ↗ | (On Diff #193318) | Somehow empty line is still there. |
Addressed reviewer feedback and also simplified the implementation, renamed the test to be more concise, and added more test cases.
docs/ReleaseNotes.rst | ||
---|---|---|
136 | Please synchronize with documentation. |
The following has been updated:
- moved check to bugprone instead of misc
- more tests
- rewritten check logic
Hey Dennis,
my 2cents on the check. I think it is very good to have! Did you check coding guidelines if they say something to this issue? (e.g. cppcoreguidelines, hicpp, cert) As we have modules for them it would be great to make aliases to this check if they demand this to be checked.
clang-tidy/bugprone/PlacementNewTargetTypeMismatch.cpp | ||
---|---|---|
22 | Please write full sentences with punctuation in comments. | |
23 | placement-new? please make a new matcher (locally) to match that, because its is more specific. | |
31 | Please ellide braces. | |
39 | braces, above the comment does not follow the correct-sentence style | |
43 | Is this universally true? What about the nothrow-overload, would that interfere? | |
46 | QualType is usually used a value-type, not const-ref. Please follow that for consistency. | |
51 | braces. | |
docs/clang-tidy/checks/bugprone-placement-new-target-type-mismatch.rst | ||
8 | Please add examples to demonstrate good and bad code for the user. | |
test/clang-tidy/bugprone-placement-new-target-type-mismatch.cpp | ||
6 | Please add a test for the nothrow overload. | |
87 | Please add test, where everything is hidden behind templates and only the type-substitution leads to the error. |
clang-tidy/bugprone/PlacementNewTargetTypeMismatch.cpp | ||
---|---|---|
39 | I would put nullptr as second argument for == operator. |
Thanks for the great suggestions. Updated the diff according to the feedback. Also checked with cppcoreguidelines, hicpp as well as cert. Only cert has a related, yet different rule stating that calls to placement new shall be provided with properly aligned pointers. I'd say this should be a distinct check. Happy to work on it after this one.
clang-tidy/bugprone/PlacementNewTargetTypeMismatch.cpp | ||
---|---|---|
43 | Thanks, rewrote that check. |
Changed the following
- addressed reviewer feedback
- fetch the placement parameter as written
- add further test cases as requested
- stylistic changes
- add nothrow parameter support
- ignore matches with unresolved template parameters
- add examples of bad and good code
What is the rationale for separating those checks? It sort of feels like there is some overlap between alignment, size, and buffer type.
I'm not certain I agree with the way this check operates -- what problem is it trying to solve? For instance, this code would trigger on this check, but is perfectly valid code:
static_assert(sizeof(int) == sizeof(float)); static_assert(alignof(int) == alignof(float)); int buffer; float *fp = new (&buffer) float;
The situations I can think of where the type mismatch is an issue would be for types with nontrivial special members, so there are times when this check makes sense, but perhaps it's too restrictive currently, but you may have a different problem in mind?
clang-tidy/bugprone/PlacementNewTargetTypeMismatch.cpp | ||
---|---|---|
20 | This should match on CXXNewExpr instead of Expr, then you won't need the dyn_cast below. | |
29 | This should probably use cxxNewExpr() instead of expr() so you match on something more specific. | |
38 | This assert message looks a bit incomplete. ;-) | |
43 | You can use const auto * here. | |
test/clang-tidy/bugprone-placement-new-target-type-mismatch.cpp | ||
56 | While this code definitely has a bug from the buffer overflow, I don't think the diagnostic is valuable here. This is a very common pattern and I suspect this will yield a lot of false positives, unless the check starts taking alignment and buffer size into consideration. |
This should match on CXXNewExpr instead of Expr, then you won't need the dyn_cast below.