-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
d2d0c8c
commit f28a428
Showing
10 changed files
with
239 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Analyzers/CSharp/Analysis/UseStringLengthInsteadOfComparisonWithEmptyStringAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Roslynator.CSharp.Syntax; | ||
|
||
namespace Roslynator.CSharp.Analysis | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class UseStringLengthInsteadOfComparisonWithEmptyStringAnalyzer : BaseDiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics | ||
{ | ||
get { return ImmutableArray.Create(DiagnosticDescriptors.UseStringLengthInsteadOfComparisonWithEmptyString); } | ||
} | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
if (context == null) | ||
throw new ArgumentNullException(nameof(context)); | ||
|
||
base.Initialize(context); | ||
|
||
context.RegisterSyntaxNodeAction(AnalyzeEqualsExpression, SyntaxKind.EqualsExpression); | ||
} | ||
|
||
private static void AnalyzeEqualsExpression(SyntaxNodeAnalysisContext context) | ||
{ | ||
var equalsExpression = (BinaryExpressionSyntax)context.Node; | ||
|
||
if (equalsExpression.ContainsDirectives) | ||
return; | ||
|
||
BinaryExpressionInfo equalsExpressionInfo = SyntaxInfo.BinaryExpressionInfo(equalsExpression); | ||
|
||
if (!equalsExpressionInfo.Success) | ||
return; | ||
|
||
ExpressionSyntax left = equalsExpressionInfo.Left; | ||
ExpressionSyntax right = equalsExpressionInfo.Right; | ||
|
||
SemanticModel semanticModel = context.SemanticModel; | ||
CancellationToken cancellationToken = context.CancellationToken; | ||
|
||
if (CSharpUtility.IsEmptyStringExpression(left, semanticModel, cancellationToken)) | ||
{ | ||
if (CSharpUtility.IsStringExpression(right, semanticModel, cancellationToken) | ||
&& !equalsExpression.IsInExpressionTree(semanticModel, cancellationToken)) | ||
{ | ||
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.UseStringLengthInsteadOfComparisonWithEmptyString, equalsExpression); | ||
} | ||
} | ||
else if (CSharpUtility.IsEmptyStringExpression(right, semanticModel, cancellationToken) | ||
&& CSharpUtility.IsStringExpression(left, semanticModel, cancellationToken) | ||
&& !equalsExpression.IsInExpressionTree(semanticModel, cancellationToken)) | ||
{ | ||
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.UseStringLengthInsteadOfComparisonWithEmptyString, equalsExpression); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.