Skip to content

Commit ae1d9ee

Browse files
authored
Merge pull request #2269 from dlemstra/SA1513
Fixed issue with properties that have an accessibility restriction.
2 parents 493390a + 3da4eb4 commit ae1d9ee

3 files changed

Lines changed: 279 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1513UnitTests.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,93 @@ public int Do(int i)
843843
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
844844
}
845845

846+
/// <summary>
847+
/// Verifies that setters with an accessibility restriction should not report a warning.
848+
/// </summary>
849+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
850+
[Fact]
851+
public async Task TestSetterWithAccessibilityRestrictionAsync()
852+
{
853+
var testCode = @"
854+
public class TestClass
855+
{
856+
public int TestProtected
857+
{
858+
get
859+
{
860+
return 1;
861+
}
862+
protected set
863+
{
864+
}
865+
}
866+
867+
public int TestInternal
868+
{
869+
get
870+
{
871+
return 1;
872+
}
873+
internal set
874+
{
875+
}
876+
}
877+
878+
public int TestPrivate
879+
{
880+
get
881+
{
882+
return 1;
883+
}
884+
private set
885+
{
886+
}
887+
}
888+
889+
public int this[int i]
890+
{
891+
get
892+
{
893+
return 1;
894+
}
895+
protected set
896+
{
897+
}
898+
}
899+
}
900+
901+
public class TestClass2
902+
{
903+
public int this[int i]
904+
{
905+
get
906+
{
907+
return 1;
908+
}
909+
internal set
910+
{
911+
}
912+
}
913+
}
914+
915+
public class TestClass3
916+
{
917+
public int this[int i]
918+
{
919+
get
920+
{
921+
return 1;
922+
}
923+
private set
924+
{
925+
}
926+
}
927+
}
928+
";
929+
930+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
931+
}
932+
846933
/// <inheritdoc/>
847934
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
848935
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1516UnitTests.cs

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,189 @@ public class TestClass
543543
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
544544
}
545545

546+
/// <summary>
547+
/// Verifies that setters with an accessibility restriction will report a warning.
548+
/// This is a regression for #2269
549+
/// </summary>
550+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
551+
[Fact]
552+
public async Task TestSetterWithAccessibilityRestrictionAsync()
553+
{
554+
var testCode = @"
555+
public class TestClass
556+
{
557+
public int TestProtected
558+
{
559+
get
560+
{
561+
return 1;
562+
}
563+
protected set
564+
{
565+
}
566+
}
567+
568+
public int TestInternal
569+
{
570+
get
571+
{
572+
return 1;
573+
}
574+
internal set
575+
{
576+
}
577+
}
578+
579+
public int TestPrivate
580+
{
581+
get
582+
{
583+
return 1;
584+
}
585+
private set
586+
{
587+
}
588+
}
589+
590+
public int this[int i]
591+
{
592+
get
593+
{
594+
return 1;
595+
}
596+
protected set
597+
{
598+
}
599+
}
600+
}
601+
602+
public class TestClass2
603+
{
604+
public int this[int i]
605+
{
606+
get
607+
{
608+
return 1;
609+
}
610+
internal set
611+
{
612+
}
613+
}
614+
}
615+
616+
public class TestClass3
617+
{
618+
public int this[int i]
619+
{
620+
get
621+
{
622+
return 1;
623+
}
624+
private set
625+
{
626+
}
627+
}
628+
}
629+
";
630+
631+
var fixedTestCode = @"
632+
public class TestClass
633+
{
634+
public int TestProtected
635+
{
636+
get
637+
{
638+
return 1;
639+
}
640+
641+
protected set
642+
{
643+
}
644+
}
645+
646+
public int TestInternal
647+
{
648+
get
649+
{
650+
return 1;
651+
}
652+
653+
internal set
654+
{
655+
}
656+
}
657+
658+
public int TestPrivate
659+
{
660+
get
661+
{
662+
return 1;
663+
}
664+
665+
private set
666+
{
667+
}
668+
}
669+
670+
public int this[int i]
671+
{
672+
get
673+
{
674+
return 1;
675+
}
676+
677+
protected set
678+
{
679+
}
680+
}
681+
}
682+
683+
public class TestClass2
684+
{
685+
public int this[int i]
686+
{
687+
get
688+
{
689+
return 1;
690+
}
691+
692+
internal set
693+
{
694+
}
695+
}
696+
}
697+
698+
public class TestClass3
699+
{
700+
public int this[int i]
701+
{
702+
get
703+
{
704+
return 1;
705+
}
706+
707+
private set
708+
{
709+
}
710+
}
711+
}
712+
";
713+
714+
var expected = new[]
715+
{
716+
this.CSharpDiagnostic().WithLocation(10, 1),
717+
this.CSharpDiagnostic().WithLocation(21, 1),
718+
this.CSharpDiagnostic().WithLocation(32, 1),
719+
this.CSharpDiagnostic().WithLocation(43, 1),
720+
this.CSharpDiagnostic().WithLocation(57, 1),
721+
this.CSharpDiagnostic().WithLocation(71, 1),
722+
};
723+
724+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
725+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
726+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
727+
}
728+
546729
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
547730
{
548731
yield return new SA1516ElementsMustBeSeparatedByBlankLine();

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,15 @@ private void AnalyzeCloseBrace(SyntaxToken token)
282282
return;
283283
}
284284

285+
if ((nextToken.IsKind(SyntaxKind.PrivateKeyword)
286+
|| nextToken.IsKind(SyntaxKind.ProtectedKeyword)
287+
|| nextToken.IsKind(SyntaxKind.InternalKeyword))
288+
&& (nextToken.Parent is AccessorDeclarationSyntax))
289+
{
290+
// the close brace is followed by an accessor with an accessibility restriction.
291+
return;
292+
}
293+
285294
var parenthesizedExpressionSyntax = nextToken.Parent as ParenthesizedExpressionSyntax;
286295
if (parenthesizedExpressionSyntax?.CloseParenToken == nextToken)
287296
{

0 commit comments

Comments
 (0)