-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang] Diagnose down casting static_cast #117914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7982,6 +7982,9 @@ def err_bad_reinterpret_cast_reference : Error< | |
| def warn_undefined_reinterpret_cast : Warning< | ||
| "reinterpret_cast from %0 to %1 has undefined behavior">, | ||
| InGroup<UndefinedReinterpretCast>, DefaultIgnore; | ||
| def warn_static_downcast : Warning< | ||
| "static downcast from %0 to %1 is potentially dangerous%select{|; did you mean 'dynamic_cast'?}2">, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is more than 'potentially dangerous', yes? Perhaps also we should mention the polymorphic-ness of the types as to why it is dangerous.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you cast to the correct derived class, it works!
Yes
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right, yes... I wish we could explain that better in the brevity we have available here. I kinda hate this is a warning with false-positives, but IMO the goal of trying to make this a deprecated cast is worth the effort. |
||
| InGroup<DiagGroup<"static-downcast">>; | ||
|
|
||
| // These messages don't adhere to the pattern. | ||
| // FIXME: Display the path somehow better. | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1759,6 +1759,16 @@ TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType, | |||||
|
|
||||||
| Self.BuildBasePathArray(Paths, BasePath); | ||||||
| Kind = CK_BaseToDerived; | ||||||
|
|
||||||
| if (!CStyle && Self.LangOpts.CPlusPlus && SrcType->getAsCXXRecordDecl()->isPolymorphic()) { | ||||||
| auto D = Self.Diag(OpRange.getBegin(), diag::warn_static_downcast) | ||||||
| << SrcType << DestType | ||||||
| << OpRange | ||||||
| << Self.LangOpts.RTTI; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there value to some sort of 'note' where we point out where the inheritence happened? Though printing the whole inheritence list is probably pretty awful. |
||||||
| if(Self.LangOpts.RTTI) | ||||||
|
||||||
| if(Self.LangOpts.RTTI) | |
| if (Self.LangOpts.RTTI) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be just 1 newline?