Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 4 additions & 11 deletions src/D2L.CodeStyle.Analyzers/AnnotationsContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#nullable disable

using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;

namespace D2L.CodeStyle.Analyzers {
Expand All @@ -20,25 +19,19 @@ public static bool TryCreate( Compilation compilation, out AnnotationsContext ct
}

private AnnotationsContext( Compilation compilation ) {
Statics = (
Audited: GetAttr( compilation, "D2L.CodeStyle.Annotations.Statics+Audited" ),
Unaudited: GetAttr( compilation, "D2L.CodeStyle.Annotations.Statics+Unaudited" )
);
Statics_Audited = GetAttr( compilation, "D2L.CodeStyle.Annotations.Statics+Audited" );
Mutability_Audited = GetAttr( compilation, "D2L.CodeStyle.Annotations.Mutability+AuditedAttribute" );
Objects = (
Immutable: GetAttr( compilation, "D2L.CodeStyle.Annotations.Objects+Immutable" ),
ImmutableBaseClass: GetAttr( compilation, "D2L.CodeStyle.Annotations.Objects+ImmutableBaseClassAttribute" ),
ConditionallyImmutable: GetAttr( compilation, "D2L.CodeStyle.Annotations.Objects+ConditionallyImmutable" ),
OnlyIf: GetAttr( compilation, "D2L.CodeStyle.Annotations.Objects+ConditionallyImmutable+OnlyIf" )
);
Mutability = (
Audited: GetAttr( compilation, "D2L.CodeStyle.Annotations.Mutability+AuditedAttribute" ),
Unaudited: GetAttr( compilation, "D2L.CodeStyle.Annotations.Mutability+UnauditedAttribute" )
);
}

internal (Attr Audited, Attr Unaudited) Statics { get; }
internal Attr Statics_Audited { get; }
internal Attr Mutability_Audited { get; }
internal (Attr Immutable, Attr ImmutableBaseClass, Attr ConditionallyImmutable, Attr OnlyIf) Objects { get; }
internal (Attr Audited, Attr Unaudited) Mutability { get; }

private static Attr GetAttr( Compilation compilation, string metadataName )
=> new( compilation.GetTypeByMetadataName( metadataName ) );
Expand Down
4 changes: 2 additions & 2 deletions src/D2L.CodeStyle.Analyzers/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public static class Diagnostics {

public static readonly DiagnosticDescriptor UnnecessaryMutabilityAnnotation = new DiagnosticDescriptor(
id: "D2L0030",
title: "Unnecessary Mutability.(Un)Audited Attribute",
messageFormat: "There is a Mutability.Audited or Mutability.Unaudited attribute on an immutable member. Remove the unnecessary attribute.",
title: "Unnecessary Mutability.Audited Attribute",
messageFormat: "There is a Mutability.Audited attribute on an immutable member. Remove the unnecessary attribute.",
category: "Cleanliness",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
Expand Down
45 changes: 7 additions & 38 deletions src/D2L.CodeStyle.Analyzers/Immutability/MutabilityAuditor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#nullable disable

using System.Linq;
using Microsoft.CodeAnalysis;
using static D2L.CodeStyle.Analyzers.Immutability.ImmutableDefinitionChecker;

Expand All @@ -15,17 +14,11 @@ out Location location
) {

// Collect audit information
var hasStaticAudited = annotationsContext.Statics.Audited.IsDefined( symbol );
var hasStaticUnaudited = annotationsContext.Statics.Unaudited.IsDefined( symbol );
var hasMutabilityAudited = annotationsContext.Mutability.Audited.IsDefined( symbol );
var hasMutabilityUnaudited = annotationsContext.Mutability.Unaudited.IsDefined( symbol );
var hasBothStaticsAttributes = hasStaticAudited && hasStaticUnaudited;
var hasBothMutabilityAttributes = hasMutabilityAudited && hasMutabilityUnaudited;
var hasEitherStaticsAttributes = hasStaticAudited || hasStaticUnaudited;
var hasEitherMutabilityAttributes = hasMutabilityAudited || hasMutabilityUnaudited;
var hasStaticAudited = annotationsContext.Statics_Audited.IsDefined( symbol );
var hasMutabilityAudited = annotationsContext.Mutability_Audited.IsDefined( symbol );

// If there are no audits, don't do anything
if( !hasEitherStaticsAttributes && !hasEitherMutabilityAttributes ) {
if( !hasStaticAudited && !hasMutabilityAudited ) {
location = null;
return false;
}
Expand All @@ -36,33 +29,11 @@ out Location location
.GetLastToken()
.GetLocation();

// Check if both static audits are applied
if( hasBothStaticsAttributes ) {
var diagnostic = Diagnostic.Create(
Diagnostics.ConflictingImmutability,
syntaxLocation,
"Statics.Audited",
"Statics.Unaudited",
symbol.Kind.ToString().ToLower() );
diagnosticSink( diagnostic );
}

// Check if both mutability audits are applied
if( hasBothMutabilityAttributes ) {
var diagnostic = Diagnostic.Create(
Diagnostics.ConflictingImmutability,
syntaxLocation,
"Mutability.Audited",
"Mutability.Unaudited",
symbol.Kind.ToString().ToLower() );
diagnosticSink( diagnostic );
}

AttributeData attr = null;

if( symbol.IsStatic ) {
// Check if a static member is using mutability audits
if( hasEitherMutabilityAttributes ) {
if( hasMutabilityAudited ) {
var diagnostic = Diagnostic.Create(
Diagnostics.InvalidAuditType,
syntaxLocation,
Expand All @@ -72,11 +43,10 @@ out Location location
diagnosticSink( diagnostic );
}

attr = annotationsContext.Statics.Audited.GetAll( symbol ).FirstOrDefault()
?? annotationsContext.Statics.Unaudited.GetAll( symbol ).FirstOrDefault();
attr = annotationsContext.Statics_Audited.GetAll( symbol ).FirstOrDefault();
} else {
// Check if a non-static member is using static audits
if( hasEitherStaticsAttributes ) {
if( hasStaticAudited ) {
var diagnostic = Diagnostic.Create(
Diagnostics.InvalidAuditType,
syntaxLocation,
Expand All @@ -86,8 +56,7 @@ out Location location
diagnosticSink( diagnostic );
}

attr = annotationsContext.Mutability.Audited.GetAll( symbol ).FirstOrDefault()
?? annotationsContext.Mutability.Unaudited.GetAll( symbol ).FirstOrDefault();
attr = annotationsContext.Mutability_Audited.GetAll( symbol ).FirstOrDefault();
}

if( attr != null ) {
Expand Down
24 changes: 0 additions & 24 deletions src/D2L.CodeStyle.Annotations/Mutability/UnauditedAttribute.cs

This file was deleted.

24 changes: 0 additions & 24 deletions src/D2L.CodeStyle.Annotations/Statics/Unaudited.cs

This file was deleted.

Loading
Loading