Skip to content

Commit

Permalink
Fix failing test for property
Browse files Browse the repository at this point in the history
  • Loading branch information
joeriddles committed Jan 9, 2024
1 parent 25c0a0c commit 0f9b281
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ static void Main(string[] args)
Id = "INTL0202",
Severity = DiagnosticSeverity.Warning,
Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior",
Locations = [new DiagnosticResultLocation("Test0.cs", 15, 36)]
Locations = [new DiagnosticResultLocation("Test0.cs", 14, 36)]
}
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
Expand Down Expand Up @@ -33,22 +34,35 @@ public override void Initialize(AnalysisContext context)

private void AnalyzeInvocation(OperationAnalysisContext context)
{
if (context.Operation is not IConversionOperation conversionOperation)
if (context.Operation is not IConversionOperation conversionOperation || !conversionOperation.Conversion.IsImplicit)
{
return;
}

if (conversionOperation.Conversion.IsImplicit && conversionOperation.Conversion.MethodSymbol is object && conversionOperation.Conversion.MethodSymbol.ContainingType is object)
if (conversionOperation.Conversion.MethodSymbol is object && conversionOperation.Conversion.MethodSymbol.ContainingType is object)
{
INamedTypeSymbol containingType = conversionOperation.Conversion.MethodSymbol.ContainingType;
INamedTypeSymbol dateTimeOffsetType = context.Compilation.GetTypeByMetadataName("System.DateTimeOffset");
if (SymbolEqualityComparer.Default.Equals(containingType, dateTimeOffsetType))
if (IsDateTimeOffsetSymbol(context, containingType))
{
context.ReportDiagnostic(Diagnostic.Create(_Rule202, conversionOperation.Syntax.GetLocation()));
}
}
else
{
bool hasImplicitArg = conversionOperation.Operand.ChildOperations
.Where(op => op.Kind == OperationKind.Argument && IsDateTimeOffsetSymbol(context, ((IArgumentOperation)op).Value.Type))
.Any();
if (hasImplicitArg)
{
context.ReportDiagnostic(Diagnostic.Create(_Rule202, conversionOperation.Syntax.GetLocation()));
}
}
}


private static bool IsDateTimeOffsetSymbol(OperationAnalysisContext context, ITypeSymbol symbol)
{
INamedTypeSymbol dateTimeOffsetType = context.Compilation.GetTypeByMetadataName("System.DateTimeOffset");
return SymbolEqualityComparer.Default.Equals(symbol, dateTimeOffsetType);
}

private static class Rule202
Expand Down

0 comments on commit 0f9b281

Please sign in to comment.