Implement SA1139: Use literal suffix notation instead of casting
See original GitHub issueNew rule proposal: Use literal suffix notation instead of casting
Category: Readability Rule: When a numeric literal is immediately cast to another numeric type, it should be replaced with a literal suffix. Rationale: Improves readability of code, and (presumably) run time casts.
Bad:
long foo = (long)50;
Good:
long foo = 50L;
See https://msdn.microsoft.com/en-us/library/aa691085(v=vs.71).aspx and https://msdn.microsoft.com/en-us/library/aa664674(v=vs.71).aspx for the specification for integer/real literals.
Things that might need considering:
The literal suffix can be upper and lower case. I would prefer uppercase, (and if you read the integer literal specification above, it recommends this).
This should work on nullable types as well:
long? foo = (long?)50;
Should be fixed to
long? foo = 50L;
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
c# - How to specify a short int literal without casting?
Short answer, No. In C#, there's no letter S that could be used as var a = 123S that would indicate that a...
Read more >Auto Fix StyleCop Warnings with Free Extensions - Mike Conrad
Warning Detail Code Formatter Code Maid
SA1001 Commas should be followed by whitespace. Some Some
SA1002 Semicolons should not be preceded by a space. Some...
Read more >The dotnet format command and StyleCop.Analyzers
This article shows how to use the dotnet format command to format your source ... Use literal suffix notation instead of casting -->...
Read more >Using Roslyn Analyzers for static code analysis | by Audacia
At Audacia we use Roslyn Analyzers to automate some of our code review processes and ... Use literal suffix notation instead of casting....
Read more >https://raw.githubusercontent.com/dotnet/runtime/8...
Elements should have the same indentation --> <Rule Id="SA1139" Action="None" /> <!-- Use literal suffix notation instead of casting --> <Rule Id="SA1200" ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve decided to make some contributions to DotNetAnalyzers during next 2 or 3 months. I want to implement this analyzer and code fix first (it seems to be relatively easy). You can track my progress on a branch at forked repo; I’ll do a pull request when I’m finished.
📝 Make sure to not report a warning on the following code: