I want to write a roslyn analyzer for string comparison. The cases are: s1 can be a string. So if s1.equals(s2) or s1 == s2; it should fix to string.equals(s1,s2,Stringcomparsion.Ordinal)
I got the basic understanding of tree and also i need to create a Analyzer file and a CodeFixProvider class.
So I tried to get the syntax tree for eg. s1.equals(s2).
Now for writing the Analyze code method, I do not know how to verify the s1 is either a string. So i need help here.
I am trying to follow this article, https://www.meziantou.net/writing-a-roslyn-analyzer.htm .
For eg.
Class{
string s1 = "one";
string s2 = "two";
bool res = one.equals(two);
}
should refactor to:
Class{
string s1 = "one";
string s2 = "two";
bool res = string.equals(one,two, StringComparsion.Ordinal);
}
You can check the implementation of the rule MA0006: GitHub
You can also check my post about string comparisons for other analyzers for strings: https://www.meziantou.net/string-comparisons-are-harder-than-it-seems.htm#getting-warnings-in
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class UseStringEqualsAnalyzer : DiagnosticAnalyzer
{
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterOperationAction(AnalyzeInvocation, OperationKind.BinaryOperator);
}
private static void AnalyzeInvocation(OperationAnalysisContext context)
{
var operation = (IBinaryOperation)context.Operation;
if (operation.OperatorKind == BinaryOperatorKind.Equals ||
operation.OperatorKind == BinaryOperatorKind.NotEquals)
{
if (operation.LeftOperand.Type.IsString() && operation.RightOperand.Type.IsString())
{
if (IsNull(operation.LeftOperand) || IsNull(operation.RightOperand))
return;
// EntityFramework Core doesn't support StringComparison and evaluates everything client side...
// https://github.com/aspnet/EntityFrameworkCore/issues/1222
if (operation.IsInExpressionArgument())
return;
context.ReportDiagnostic(s_rule, operation, $"{operation.OperatorKind} operator");
}
}
}
private static bool IsNull(IOperation operation)
{
return operation.ConstantValue.HasValue && operation.ConstantValue.Value == null;
}
}
Note that if you're just looking to have this analyzer to run on your own code and less interested in actually writing it, this is already implemented in the Microsoft-written analyzers:
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1307
Instructions for installation are available here:
https://github.com/dotnet/roslyn-analyzers#microsoftcodeanalysisfxcopanalyzers
Our code for it is also available, but @meziantou's answer is a good example if you do want the simple code to start to understand analyzers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.