How to be warned about possible arithmetic errors due to type conversion?

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

double v = 4 / 100; 

I know this is incorrect initialization, which returns v = 0.0 instead of v = 0.04

In C # rules, I have to ensure that at least one of the members is double , for example:

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

However, I have many initializations of this kind that involve operations with integer variables, and I feel lazy to look at my code in turn to detect such errors.

Instead, can the compiler be warned about this?

+8
math c # compiler-warnings implicit-conversion arithmetic-expressions
source share
2 answers

Well, after some games, and what not, I have a solution. I used this article to come to this decision. I use StyleCop , so you need to install and install it. Then you can download my C # MathematicsAnalyzer project.

Firstly, I did not take into account all the inconsistencies of type conversion. In fact, I only fit one piece.

Basically, I check to see if the string contains "double" and then a space. I know that this can lead to false warnings, because the end of the class can be double or any other, but I will leave it to you to figure out how to properly isolate the type.

If a match is found, I check that it matches this regular expression:

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

If this does not match this regular expression, I add a violation. What must match this regular expression 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 actions will not create a violation. As it is currently written, to a large extent, if "d" is not used, it will cause a violation. You will need to add additional logic to account for other possible ways to explicitly cast the operand. When I write this, I only realized that having a ā€œdā€ on both operands would most likely throw an exception. Oops

And finally, I was not able to get StyleCop to correctly display my violation. This all the time gave me an error that the rule does not exist, and even with the second pair of eyes on it, we could not find a solution, so I cracked 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 a line number in it.

To set up a custom rule, create a MathematicalAnalyzer project. Close Visual Studio and copy the DLL to the StyleCop installation directory. When you open Visual Studio, you should see the rule in the StyleCop settings. In steps 5 and 6 of the article, I show where to do this.

This solution receives only one violation at a time throughout the solution, so you will have to fix the violation and start StyleCop again to find the next one. Perhaps it will be so, but I ran out of the juice and stayed here.

Enjoy it!

+4
source share

This article explains how to set up custom code analysis rules that, when running Code Analysis, can display 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 /

+3
source share

All Articles