From 2b6245cb9cd9c9cc0662369fd303c830b0e5ec91 Mon Sep 17 00:00:00 2001 From: milleniumbug Date: Sat, 2 Jun 2018 15:40:37 +0200 Subject: [PATCH] Don't warn on constructor assignment to C#6 virtual readonly properties --- ...allOverridableMethodsInConstructorAnalyzer.cs | 4 +++- ...otCallOverridableMethodsInConstructorTests.cs | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/RefactoringEssentials/CSharp/Diagnostics/Synced/CodeQuality/DoNotCallOverridableMethodsInConstructorAnalyzer.cs b/RefactoringEssentials/CSharp/Diagnostics/Synced/CodeQuality/DoNotCallOverridableMethodsInConstructorAnalyzer.cs index 26b9c61c..cbd0a56d 100644 --- a/RefactoringEssentials/CSharp/Diagnostics/Synced/CodeQuality/DoNotCallOverridableMethodsInConstructorAnalyzer.cs +++ b/RefactoringEssentials/CSharp/Diagnostics/Synced/CodeQuality/DoNotCallOverridableMethodsInConstructorAnalyzer.cs @@ -90,7 +90,9 @@ void Check(SyntaxNode n, bool skipMethods) if (n.Ancestors().Any(a => a is AssignmentExpressionSyntax)) { var setterMethodSymbol = propertySymbol.SetMethod; - if ((setterMethodSymbol != null) && (setterMethodSymbol.DeclaredAccessibility == Accessibility.Private)) + if (setterMethodSymbol == null) + return; + if (setterMethodSymbol.DeclaredAccessibility == Accessibility.Private) return; } else diff --git a/Tests/CSharp/Diagnostics/DoNotCallOverridableMethodsInConstructorTests.cs b/Tests/CSharp/Diagnostics/DoNotCallOverridableMethodsInConstructorTests.cs index c65ebc1c..8543f45d 100644 --- a/Tests/CSharp/Diagnostics/DoNotCallOverridableMethodsInConstructorTests.cs +++ b/Tests/CSharp/Diagnostics/DoNotCallOverridableMethodsInConstructorTests.cs @@ -316,6 +316,20 @@ public Test () { } }"); } - + + [Fact] + public void DoNotWarnOnReadOnlyProperties() + { + Analyze(@" class Foo +{ + Foo() + { + AutoProperty = 42; + } + + public virtual int AutoProperty { get; } +"); + } + } }