question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement SA1139: Use literal suffix notation instead of casting

See original GitHub issue

New 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:closed
  • Created 8 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tmaczynskicommented, Jun 8, 2016

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.

1reaction
sharwellcommented, Feb 9, 2016

📝 Make sure to not report a warning on the following code:

var foo = (long?)50;
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found