Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rule S6566: Always use DateTimeOffset instead of DateTime #7523

Merged
merged 18 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove abstract methods
  • Loading branch information
CristianAmbrosini committed Jul 4, 2023
commit 17f617ac53fb3246c7fc42f3d0e6d2ac5f7135f6
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@
namespace SonarAnalyzer.Rules.CSharp;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class UseDateTimeInsteadOfDateTimeOffset : UseDateTimeInsteadOfDateTimeOffsetBase<SyntaxKind, MemberAccessExpressionSyntax>
public sealed class UseDateTimeInsteadOfDateTimeOffset : UseDateTimeInsteadOfDateTimeOffsetBase<SyntaxKind>
{
protected override ILanguageFacade<SyntaxKind> Language => CSharpFacade.Instance;

protected override bool IsNamedDateTime(SyntaxNode node) =>
Language.GetName(node).Equals("DateTime", Language.NameComparison);
protected override string[] ValidNames { get; } = new[] { "DateTime" };

protected override SyntaxNode GetType(SyntaxNode node) =>
((ObjectCreationExpressionSyntax)node).Type;

protected override Location GetExpressionLocation(MemberAccessExpressionSyntax node) =>
node.Expression.GetLocation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

namespace SonarAnalyzer.Rules;

public abstract class UseDateTimeInsteadOfDateTimeOffsetBase<TSyntaxKind, TMemberAccess> : SonarDiagnosticAnalyzer<TSyntaxKind>
public abstract class UseDateTimeInsteadOfDateTimeOffsetBase<TSyntaxKind> : SonarDiagnosticAnalyzer<TSyntaxKind>
where TSyntaxKind : struct
where TMemberAccess : SyntaxNode
{
private const string DiagnosticId = "S6566";

Expand All @@ -36,9 +35,8 @@ public abstract class UseDateTimeInsteadOfDateTimeOffsetBase<TSyntaxKind, TMembe
nameof(DateTime.UtcNow),
"UnixEpoch");

protected abstract bool IsNamedDateTime(SyntaxNode node);
protected abstract string[] ValidNames { get; }
protected abstract SyntaxNode GetType(SyntaxNode node);
protected abstract Location GetExpressionLocation(TMemberAccess node);

protected UseDateTimeInsteadOfDateTimeOffsetBase() : base(DiagnosticId) { }

Expand All @@ -65,12 +63,15 @@ protected sealed override void Initialize(SonarAnalysisContext context)
&& TargetMemberAccess.Contains(Language.GetName(c.Node))
&& IsDateTimeType(expression, c.SemanticModel))
{
c.ReportIssue(Diagnostic.Create(Rule, GetExpressionLocation((TMemberAccess)c.Node)));
c.ReportIssue(Diagnostic.Create(Rule, Language.Syntax.NodeExpression(c.Node).GetLocation()));
}
},
Language.SyntaxKind.SimpleMemberAccessExpression);
}

private static bool IsDateTimeType(SyntaxNode node, SemanticModel model) =>
model.GetTypeInfo(node).Type.Is(KnownType.System_DateTime);

private bool IsNamedDateTime(SyntaxNode node) =>
Array.Exists(ValidNames, x => x.Equals(Language.GetName(node), Language.NameComparison));
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@
namespace SonarAnalyzer.Rules.VisualBasic;

[DiagnosticAnalyzer(LanguageNames.VisualBasic)]
public sealed class UseDateTimeInsteadOfDateTimeOffset : UseDateTimeInsteadOfDateTimeOffsetBase<SyntaxKind, MemberAccessExpressionSyntax>
public sealed class UseDateTimeInsteadOfDateTimeOffset : UseDateTimeInsteadOfDateTimeOffsetBase<SyntaxKind>
{
protected override ILanguageFacade<SyntaxKind> Language => VisualBasicFacade.Instance;

private ImmutableArray<string> validNames = ImmutableArray.Create("DateTime", "Date");

protected override bool IsNamedDateTime(SyntaxNode node) =>
validNames.Contains(Language.GetName(node));
protected override string[] ValidNames { get; } = new[] { "DateTime", "Date" };

protected override SyntaxNode GetType(SyntaxNode node) =>
((ObjectCreationExpressionSyntax)node).Type;

protected override Location GetExpressionLocation(MemberAccessExpressionSyntax node) =>
node.Expression.GetLocation();
}