How to generate an alert if a specific enumeration value is used

I am using an enum which can be shown by a third party assembly, for example

public enum APIEnum
{
  Val1,
  Val2
}

      

... However, some of these values ​​are causing incorrect behavior in my application. I want to generate a compiler warning if my code uses one of these "bad" enum values, for example

APIEnum usedVal = APIEnum.Val2;

Compiler Warning: APIEnum.Val2 causes incorrect behavior.

      

My ultimate goal is to create a warning to be deliberately # pragma'd if a bad value is used (2% of total cases). Otherwise, a warning is thrown, and since we have Warnings as Errors, this breaks compilation until fixed or # pragma'd.

I've covered the topics here and here about using the Obsolete attribute to solve this problem, but I'm afraid Obsolete will cause confusion as the value is not deprecated.

I considered using the Resharper code analysis plugin to solve the problem and this is definitely an option. I am not an expert on Resharper or what is the best way to solve the problem with Resharper.

+3


source to share


3 answers


You can use Structured Search in ReSharper. Go to ReSharper -> Options | Code Inspection -> Custom patterns

, press Add Pattern

, enter APIEnum.Val2

in the field Search pattern

and your description of the error in Description

. Set the severity of the drawing to Show as error

. Click Add

. All this. The only drawback is that if you have another one APIEnum

with the same value Val2

in your project, even in a different namespace, then it will also be flagged as an error.



You must also enable ReSharper -> Options | Code Inspection -> Settings | Analyse errors in whole solution

to show errors in every file.

+2


source


To do this, you can create your own Code Analysis (FxCop) rule, or indeed collapse your own Resharper rule. The custom parsing code rule should be relatively simple, check my rule that checks if the regex compiles , it finds all uses of the RegexOptions

enum. You should be able to create your own rule.

Common great sites for custom code parsing rules:

If you get stuck writing your own rule, feel free to share this code to request more specific help.



Use a StatementAssignment Statement and then use assignment.Target.Type.FullName

to get the underlying type of the enum. Make sure Target.Type is not null, delegates can be null.

Introspector showing Enum Type

Enum will also appear in Methodcall:

Introspector showing Assignment and Methodcall

+1


source


I prototyped a solution using FxCop's approach and I don't think FxCop can solve it. I've tried the following code in the rule:

public class DoNotUseSpecificEnum : RuleBase
{
  private string[] _enumValsToCheck =
  {
    "APIEnum.Val2"
  };

  public DoNotUseSpecificEnum ()
    : base("DoNotUseSpecificEnum ") { }

  public override void VisitBinaryExpression(BinaryExpression binaryExpression)
  {
    if ( _enumValsToCheck.Contains(
      binaryExpression.Operand1.ToString()) 
      || _enumValsToCheck.Contains( binaryExpression.Operand2.ToString() ) )
    {
      this.Problems.Add(new Problem(base.GetResolution(),
        binaryExpression.SourceContext));
    }

    base.VisitBinaryExpression(binaryExpression);
  }
}

      

When I trace to VisitBinaryExpression, I believe Operand1 will be "1" instead of "APIEnum.Val2". This makes sense in retrospect (FxCop is working on MSIL, which replaced enumeration names / values ​​with numeric literals); I just wish I knew it before I dug into the pain / glory of FxCop custom rules. :)

Another approach I found was to use StyleCop for parsing, but this looks even harder to define and less popular than custom FxCop Rules.

I'm going to suggest using the custom templates defined in the Resharper team as a way to manage this. I'm still worried about this approach (especially since we'll stick to the same rules for multiple decisions and commands), but the build layout issue is much smaller than I thought. TeamCity allows you to check the execution time of Resharper rules , so we can at least configure our CI server to find and report on enumeration usage that hasn't been suppressed using Resharper syntax.

0


source







All Articles