-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Proposal: Add optional warning for implicit runtime casts #14382
Comments
I think this sounds like something that would be suitable for an analyzer. It's not something that's likely an error in your code, so I think that a warning built into the compiler is not suitable. But it is something that you might reasonably decide to not allow in your code style. So I think an analyzer would be ideal. |
It’s something that is likely an error after refactoring. For example, you may have an It would be fine as an analyzer because that can be used to generate warnings too. I am thinking more about this and maybe writing analyzers is easy enough that even I could do it. Don’t know if/when I’ll get to it, though. However, I do have a concern about writing this as an analyzer. Depending on the Roslyn API, which I have not looked into yet, it might be impossible for an analyzer to hook into all places where implicit runtime casts happen. I only pointed out two places that I know of this happening:
|
Thought that I’d update here to mention that I have C# It is hardcoded to emit a warning. As suggested earlier in this thread, I can make this into a compilation error with: <PropertyGroup>
<WarningsAsErrors>OhNoPubImplicitCastForeach</WarningsAsErrors>
</PropertyGroup> It also has a codefix which is hardcoded to provide Criticism/suggestions/PRs (as long as I can be convinced of them ;-)) welcome, including for the horrendous typos in the README which I just haven’t gotten around to fixing :-/. |
Version Used:
Visual Studio 15 Preview 4 (unsure of how to find what version of roslyn it uses).
Steps to Reproduce:
Set warning level to 4 (maximum).
Compile a program which has implicit casts such as the following:
Observe that there are no warnings and yet runtime exceptions occur at each place where an implicit cast was used.
Expected Behavior:
I’d like to have compiler support in eliminating accidental usage of implicit casts from my codebase (e.g., something to pass to
/warnaserror
). These casts are not always obvious and they don’t always compile to runtime casts, but when they do they can result in errors that are not seen until runtime. One great strength of C# is its static typing. But if the language makes it so easy to accidentally write code that emits runtime casts, without even an optional warning, the compiler’s support for static typing is wasted.I understand that a lot of people rely on these constructs to use legacy APIs which use non-generic collections. Thus I suggest that this new warning be made opt-in. I don’t know the best way to go about that though…
I’d like usage of the construct
foreach («TypeName» item in collection)
to emit this warning if it compiles to a runtime cast and forfrom «TypeName» item in collection select item
to emit the same warning if it compiles to a call toCast<«typeName»>()
. Something like a message describing both the target and source types of the implicit cast:Or for a more real-life example:
Actual Behavior:
No warnings.
The text was updated successfully, but these errors were encountered: