How to be warned about potential arithmetic errors

2020-02-09 12:12发布

问题:

I am working on a calculation module using C#, and I bumped on this :

double v = 4 / 100;

I know this is a wrong initialization that returns v = 0.0 instead of v = 0.04

The c# rules says I must ensure at least one of the member is a double, like this :

double v = (double) 4 / 100;
double v = 4.0 / 100;

However, I have many many initializations of that kind that involves integer variables operations, and I feel lazy to browse my code line by line to detect such mistakes.

Instead, is it possible to get warned by the compiler about this ?

回答1:

Alright, after some playing around and what not, I have a solution. I used this article to come to this solution.I use StyleCop, so you'll need to get and install that. Then, you can download my C# project MathematicsAnalyzer.

First off, I did not account for all type conversion mismatches. In fact, I only accommodate one part.

Basically, I check to see if the line contains "double" followed by a space. I do know that could lead to false warnings, because the end of a class could be double or any number of other things, but I'll leave that to you to figure out how to properly isolate the type.

If a match is found, I check to see that it matches this regex:

double[ ][A-Za-z0-9]*[ ]?=(([ ]?[0-9]*d[ ]?/[ ]?[0-9]*;)|[ ]?[0-9]*[ ]?/[ ]?[0-9]*d;)

If it does -not- match this regex, then I add a violation. What this regex will match on is any of the following:

  • double i=4d / 100;
  • double i = 4d / 100;
  • double i = 4 / 100d;
  • double i = 4/ 100d;
  • double i = 4 /100d;
  • double i = 4/100d;
  • double i=4d / 100;
  • double i=4 / 100d;
  • double i=4/100d;

Any of the above will not create a violation. As it is currently written, pretty much if a 'd' isn't used, it'll throw a violation. You'll need to add extra logic to account for the other possible ways of explicitly casting an operand. As I'm writing this, I've just realized that having a 'd' on both operands will most likely throw an exception. Whoops.

And lastly, I could not get StyleCop to display my violation properly. It kept giving me an error about the rule not existing, and even with a second pair of eyes on it, we could not find a solution, so I hacked it. The error shows the name of the rule you were trying to find, so I just put the name of the rule as something descriptive and included the line number in it.

To install the custom rule, build the MathematicalAnalyzer project. Close Visual Studio and copy the DLL into the StyleCop install directory. When you open Visual Studio, you should see the rule in the StyleCop settings. Step 5 and 6 of the article I used shows where to do that.

This only gets one violation at a time throughout the solution, so you'll have to fix the violation it shows, and run StyleCop again to find the next one. There may be a way around that, but I ran out of juice and stopped here.

Enjoy!



回答2:

This article explains how to set up custom Code Analysis rules that, when you run Code Analysis, can show warnings and what not.

http://blog.tatham.oddie.com.au/2010/01/06/custom-code-analysis-rules-in-vs2010-and-how-to-make-them-run-in-fxcop-and-vs2008-too/