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!