[英]Roslyn Analyzer for String comparison
I want to write a roslyn analyzer for string comparison.我想编写一个用于字符串比较的 roslyn 分析器。 The cases are: s1 can be a string.这些情况是: s1 可以是一个字符串。 So if s1.equals(s2) or s1 == s2;所以如果 s1.equals(s2) 或 s1 == s2; it should fix to string.equals(s1,s2,Stringcomparsion.Ordinal)它应该修复为 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.我对树有了基本的了解,还需要创建一个分析器文件和一个 CodeFixProvider class。
So I tried to get the syntax tree for eg.所以我试图获取例如语法树。 s1.equals(s2). s1.等于(s2)。
Now for writing the Analyze code method, I do not know how to verify the s1 is either a string.现在为了编写分析代码方法,我不知道如何验证 s1 是否为字符串。 So i need help here.所以我在这里需要帮助。
I am trying to follow this article, https://www.meziantou.net/writing-a-roslyn-analyzer.htm .我正在尝试关注这篇文章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您可以查看规则 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您还可以查看我关于其他字符串分析器的字符串比较的帖子: 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:请注意,如果您只是希望让此分析器在您自己的代码上运行并且对实际编写它不太感兴趣,那么这已经在 Microsoft 编写的分析器中实现:
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1307 https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1307
Instructions for installation are available here:安装说明可在此处获得:
https://github.com/dotnet/roslyn-analyzers#microsoftcodeanalysisfxcopanalyzers 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.我们的代码也可用,但如果您确实希望简单的代码开始理解分析器,@meziantou 的回答就是一个很好的例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.