From df4be680de5c6fef92ce10fbe56fad66ed0641d4 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Tue, 30 Jul 2019 00:19:38 +0300 Subject: [PATCH 01/16] Implement ReferingToObjectAndReassigningItInTheSameStatement analyzer --- ...ftQualityGuidelinesAnalyzersResources.resx | 60 ++-- ...bjectAndReassigningItInTheSameStatement.cs | 96 ++++++ ...QualityGuidelinesAnalyzersResources.cs.xlf | 10 + ...QualityGuidelinesAnalyzersResources.de.xlf | 10 + ...QualityGuidelinesAnalyzersResources.es.xlf | 10 + ...QualityGuidelinesAnalyzersResources.fr.xlf | 10 + ...QualityGuidelinesAnalyzersResources.it.xlf | 10 + ...QualityGuidelinesAnalyzersResources.ja.xlf | 10 + ...QualityGuidelinesAnalyzersResources.ko.xlf | 10 + ...QualityGuidelinesAnalyzersResources.pl.xlf | 10 + ...lityGuidelinesAnalyzersResources.pt-BR.xlf | 10 + ...QualityGuidelinesAnalyzersResources.ru.xlf | 10 + ...QualityGuidelinesAnalyzersResources.tr.xlf | 10 + ...tyGuidelinesAnalyzersResources.zh-Hans.xlf | 10 + ...tyGuidelinesAnalyzersResources.zh-Hant.xlf | 10 + ...AndReassigningItInTheSameStatementTests.cs | 277 ++++++++++++++++++ 16 files changed, 536 insertions(+), 27 deletions(-) create mode 100644 src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatement.cs create mode 100644 src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatementTests.cs diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx index d3ca16204b..9fdd120a2f 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx @@ -1,17 +1,17 @@ - @@ -252,4 +252,10 @@ The property {0} should not be assigned to itself. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + Refering to object and reassigning it in the same statement. + \ No newline at end of file diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatement.cs b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatement.cs new file mode 100644 index 0000000000..19ba5eb276 --- /dev/null +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatement.cs @@ -0,0 +1,96 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Immutable; +using Analyzer.Utilities; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Operations; + +namespace Microsoft.CodeQuality.Analyzers.QualityGuidelines +{ + /// + /// ReferenceChanged: Prevent objects from being referenced in statements where they are reassigned + /// + [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] + + public sealed class ReferingToObjectAndReassigningItInTheSameStatement : DiagnosticAnalyzer + { + internal const string RuleId = "ReferenceChanged"; + + private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.ReferingToObjectAndReassigningItInTheSameStatementTitle), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); + private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.ReferingToObjectAndReassigningItInTheSameStatementMessage), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); + + internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(RuleId, + s_localizableTitle, + s_localizableMessage, + DiagnosticCategory.Usage, + DiagnosticHelpers.DefaultDiagnosticSeverity, + isEnabledByDefault: DiagnosticHelpers.EnabledByDefaultIfNotBuildingVSIX, + helpLinkUri: null); + + public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule); + + public override void Initialize(AnalysisContext context) + { + context.RegisterOperationAction(AnalyzeAssignment, OperationKind.SimpleAssignment); + } + + private void AnalyzeAssignment(OperationAnalysisContext context) + { + var assignmentOperation = (ISimpleAssignmentOperation)context.Operation; + + // Check if there are more then one assignment in a statement + if (!(assignmentOperation.Target is IMemberReferenceOperation operationTarget)) + { + return; + } + + // This analyzer makes sense only for reference type objects + if (operationTarget.Instance?.Type.IsValueType == true) + { + return; + } + + // Search for object equal to operationTarget.Instance further in assignment chain + bool isViolationFound = false; + string violatingObjectName; + if (operationTarget.Instance is ILocalReferenceOperation localInstance) + { + violatingObjectName = localInstance.Local.Name; + isViolationFound = AnalyzeMemberAssignment(assignmentOperation, localInstance, (a, b) => a.Local == b.Local); + } + else if (operationTarget.Instance is IMemberReferenceOperation memberInstance) + { + violatingObjectName = memberInstance.Member.Name; + isViolationFound = AnalyzeMemberAssignment(assignmentOperation, memberInstance, (a, b) => a.Member == b.Member); + } + else + { + return; + } + + if (isViolationFound) + { + var diagnostic = Diagnostic.Create(Rule, operationTarget.Syntax.GetLocation(), violatingObjectName); + context.ReportDiagnostic(diagnostic); + } + } + + private static bool AnalyzeMemberAssignment(ISimpleAssignmentOperation assignmentOperation, T instance, Func equalityComparer) where T : class, IOperation + { + // Check every simple assignments target in a statement for equality to `instance` + while (assignmentOperation.Value != null && assignmentOperation.Value.Kind == OperationKind.SimpleAssignment) + { + assignmentOperation = (ISimpleAssignmentOperation)assignmentOperation.Value; + + var operationValue = assignmentOperation.Target as T; + if (equalityComparer(instance, operationValue)) + { + return true; + } + } + return false; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf index b6f0b46cec..63769d2163 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf @@ -22,6 +22,16 @@ Některé odkazy na {0} nešlo opravit. Měly by se opravit ručně. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Tam, kde je to vhodné, použijte literály diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf index f83a1173f8..433184e5cd 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf @@ -22,6 +22,16 @@ Einige Verweise auf "{0}" konnten nicht korrigiert werden. Korrigieren Sie sie manuell. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Nach Möglichkeit Literale verwenden diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf index b6098d635e..2c6b34e5d7 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf @@ -22,6 +22,16 @@ Algunas referencias a "{0}" podrían no corregirse, así que tendría que hacerlo manualmente. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Usar literales cuando resulte apropiado diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf index b876656d1b..175f988bce 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf @@ -22,6 +22,16 @@ Des références à '{0}' n'ont pas pu être corrigées, elles doivent être corrigées manuellement. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Utiliser des littéraux quand cela est approprié diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf index e63e15321a..ad6f701c0f 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf @@ -22,6 +22,16 @@ Non è stato possibile correggere alcuni riferimenti a '{0}'. Correggerli manualmente. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Usa valori letterali dove appropriato diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf index 428c32284f..7d4b6d3ff9 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf @@ -22,6 +22,16 @@ '{0}' への参照を修正できませんでした。手動で修正する必要があります。 + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate 適切な場所にリテラルを使用します diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf index 5348c47c54..bc6c9d17e9 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf @@ -22,6 +22,16 @@ '{0}'에 대한 일부 참조를 수정할 수 없습니다. 수동으로 수정해야 합니다. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate 적합한 리터럴을 사용하세요. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf index 89ca2e2d45..fa8796612e 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf @@ -22,6 +22,16 @@ Nie można naprawić niektórych odwołań do „{0}”. Należy je naprawić ręcznie. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Używaj literałów w odpowiednich miejscach diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf index c10c48e8f3..99eb002875 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf @@ -22,6 +22,16 @@ Algumas referências a '{0}' não puderam ser corrigidas, elas devem ser corrigidas manualmente. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Usar literais sempre que apropriado diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf index fbe902cbce..ee0585f23c 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf @@ -22,6 +22,16 @@ Не удалось исправить некоторые ссылки на "{0}". Их следует исправить вручную. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Используйте литералы, когда это уместно diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf index 9b4471ece2..b3f42d10b2 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf @@ -22,6 +22,16 @@ Bazı '{0}' başvuruları düzeltilemedi, bunları kendiniz düzeltmelisiniz. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Uygun durumlarda sabit değerler kullanın diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf index 40d74abaf1..c274ddc3b4 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf @@ -22,6 +22,16 @@ 无法修复对“{0}”的部分引用,应手动修复它们。 + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate 在合适的位置使用文本 diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf index c9b22e7b3e..bf70969d41 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf @@ -22,6 +22,16 @@ 無法修正對 '{0}' 的某些參考,應予以手動修正。 + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate 適當時機使用常值 diff --git a/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatementTests.cs b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatementTests.cs new file mode 100644 index 0000000000..0d52cc54c6 --- /dev/null +++ b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatementTests.cs @@ -0,0 +1,277 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing; +using Microsoft.CodeQuality.Analyzers.QualityGuidelines; +using Test.Utilities; +using Xunit; + +namespace Microsoft.CodeQuality.Analyzers.UnitTests.QualityGuidelines +{ + public partial class ReferingToObjectAndReassigningItInTheSameStatementTests : DiagnosticAnalyzerTestBase + { + protected override DiagnosticAnalyzer GetBasicDiagnosticAnalyzer() + { + return new ReferingToObjectAndReassigningItInTheSameStatement(); + } + + protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() + { + return new ReferingToObjectAndReassigningItInTheSameStatement(); + } + + private DiagnosticResult GetCSharpResultAt(int line, int column, string symbolName) + { + return GetCSharpResultAt(line, column, ReferingToObjectAndReassigningItInTheSameStatement.Rule, symbolName); + } + + private DiagnosticResult GetBasicResultAt(int line, int column, string symbolName) + { + return GetBasicResultAt(line, column, ReferingToObjectAndReassigningItInTheSameStatement.Rule, symbolName); + } + + [Fact] + public void CSharpReassignLocalVariableAndReferToItsField() + { + VerifyCSharp(@" +public class C +{ + public C Field; +} + +public class Test +{ + public void Method() + { + C a, b; + a.Field = a = b; + } +} +", + GetCSharpResultAt(12, 9, "a")); + } + + [Fact] + public void CSharpReassignLocalVariableAndReferToItsProperty() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + +public class Test +{ + public void Method() + { + C a, b, c; + a.Property = c = a = b; + } +} +", + GetCSharpResultAt(12, 9, "a")); + } + + [Fact] + public void CSharpReassignLocalVariablesPropertyAndReferToItsProperty() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + +public class Test +{ + public void Method() + { + C a, b; + a.Property.Property = a.Property = b; + } +} +", + GetCSharpResultAt(12, 9, "Property")); + } + + [Fact] + public void CSharpReassignLocalVariableAndItsPropertyAndReferToItsProperty() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + +public class Test +{ + public void Method() + { + C a, b; + a.Property.Property = a.Property = a = b; + } +} +", + GetCSharpResultAt(12, 9, "Property"), + GetCSharpResultAt(12, 31, "a")); + } + + [Fact] + public void CSharpReferToFieldOfReferenceTypeLocalVariableAfterItsReassignment() + { + VerifyCSharp(@" +public class C +{ + public C Field; +} + + +public class Test +{ + static C x, y; + + public void Method() + { + x.Field = x = y; + } +} +", + GetCSharpResultAt(14, 9, "x")); + } + + [Fact] + public void CSharpReassignGlobalVariableAndReferToItsField() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + + +public class Test +{ + static C x, y; + + public void Method() + { + x.Property.Property = x.Property = y; + } +} +", + GetCSharpResultAt(14, 9, "Property")); + } + + [Fact] + public void CSharpReassignGlobalVariableAndItsPropertyAndReferToItsProperty() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + + +public class Test +{ + static C x, y; + + public void Method() + { + x.Property.Property = x.Property = x = y; + } +} +", + GetCSharpResultAt(14, 9, "Property"), + GetCSharpResultAt(14, 31, "x")); + } + + + [Fact] + public void CSharpReassignGlobalPropertyAndItsPropertyAndReferToItsProperty() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + + +public class Test +{ + static C x { get; set; } + static C y { get; set; } + + public void Method() + { + x.Property.Property = x.Property = x = y; + } +} +", + GetCSharpResultAt(15, 9, "Property"), + GetCSharpResultAt(15, 31, "x")); + } + + [Fact] + public void CSharpReassignSecondLocalVariableAndReferToItsPropertyOfFirstVariable() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + + +public class Test +{ + public void Method() + { + C a, b; + a.Property = b = a; + } +} +"); + } + + [Fact] + public void CSharpReassignLocalValueTypeVariableAndReferToItsField() + { + VerifyCSharp(@" +public struct S +{ + public S Property { get; set; } +} + + +public class Test +{ + public void Method() + { + S a, b; + a.Field = a = b; + } +} +"); + } + + [Fact] + public void CSharpReassignLocalValueTypeVariableAndReferToItsProperty() + { + VerifyCSharp(@" +public struct S +{ + public S Field; +} + + +public class Test +{ + public void Method() + { + S a, b; + a.Property = c = a = b; + } +} +"); + } + } +} From 0ca0a63a2adf41599d784fa101116975982bfaac Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Tue, 30 Jul 2019 00:19:38 +0300 Subject: [PATCH 02/16] Implement ReferingToObjectAndReassigningItInTheSameStatement analyzer --- ...ftQualityGuidelinesAnalyzersResources.resx | 60 ++-- ...bjectAndReassigningItInTheSameStatement.cs | 96 +++++++ ...QualityGuidelinesAnalyzersResources.cs.xlf | 10 + ...QualityGuidelinesAnalyzersResources.de.xlf | 10 + ...QualityGuidelinesAnalyzersResources.es.xlf | 10 + ...QualityGuidelinesAnalyzersResources.fr.xlf | 10 + ...QualityGuidelinesAnalyzersResources.it.xlf | 10 + ...QualityGuidelinesAnalyzersResources.ja.xlf | 10 + ...QualityGuidelinesAnalyzersResources.ko.xlf | 10 + ...QualityGuidelinesAnalyzersResources.pl.xlf | 10 + ...lityGuidelinesAnalyzersResources.pt-BR.xlf | 10 + ...QualityGuidelinesAnalyzersResources.ru.xlf | 10 + ...QualityGuidelinesAnalyzersResources.tr.xlf | 10 + ...tyGuidelinesAnalyzersResources.zh-Hans.xlf | 10 + ...tyGuidelinesAnalyzersResources.zh-Hant.xlf | 10 + ...AndReassigningItInTheSameStatementTests.cs | 270 ++++++++++++++++++ 16 files changed, 529 insertions(+), 27 deletions(-) create mode 100644 src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatement.cs create mode 100644 src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatementTests.cs diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx index d3ca16204b..9fdd120a2f 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx @@ -1,17 +1,17 @@ - @@ -252,4 +252,10 @@ The property {0} should not be assigned to itself. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + Refering to object and reassigning it in the same statement. + \ No newline at end of file diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatement.cs b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatement.cs new file mode 100644 index 0000000000..19ba5eb276 --- /dev/null +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatement.cs @@ -0,0 +1,96 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Immutable; +using Analyzer.Utilities; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Operations; + +namespace Microsoft.CodeQuality.Analyzers.QualityGuidelines +{ + /// + /// ReferenceChanged: Prevent objects from being referenced in statements where they are reassigned + /// + [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] + + public sealed class ReferingToObjectAndReassigningItInTheSameStatement : DiagnosticAnalyzer + { + internal const string RuleId = "ReferenceChanged"; + + private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.ReferingToObjectAndReassigningItInTheSameStatementTitle), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); + private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.ReferingToObjectAndReassigningItInTheSameStatementMessage), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); + + internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(RuleId, + s_localizableTitle, + s_localizableMessage, + DiagnosticCategory.Usage, + DiagnosticHelpers.DefaultDiagnosticSeverity, + isEnabledByDefault: DiagnosticHelpers.EnabledByDefaultIfNotBuildingVSIX, + helpLinkUri: null); + + public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule); + + public override void Initialize(AnalysisContext context) + { + context.RegisterOperationAction(AnalyzeAssignment, OperationKind.SimpleAssignment); + } + + private void AnalyzeAssignment(OperationAnalysisContext context) + { + var assignmentOperation = (ISimpleAssignmentOperation)context.Operation; + + // Check if there are more then one assignment in a statement + if (!(assignmentOperation.Target is IMemberReferenceOperation operationTarget)) + { + return; + } + + // This analyzer makes sense only for reference type objects + if (operationTarget.Instance?.Type.IsValueType == true) + { + return; + } + + // Search for object equal to operationTarget.Instance further in assignment chain + bool isViolationFound = false; + string violatingObjectName; + if (operationTarget.Instance is ILocalReferenceOperation localInstance) + { + violatingObjectName = localInstance.Local.Name; + isViolationFound = AnalyzeMemberAssignment(assignmentOperation, localInstance, (a, b) => a.Local == b.Local); + } + else if (operationTarget.Instance is IMemberReferenceOperation memberInstance) + { + violatingObjectName = memberInstance.Member.Name; + isViolationFound = AnalyzeMemberAssignment(assignmentOperation, memberInstance, (a, b) => a.Member == b.Member); + } + else + { + return; + } + + if (isViolationFound) + { + var diagnostic = Diagnostic.Create(Rule, operationTarget.Syntax.GetLocation(), violatingObjectName); + context.ReportDiagnostic(diagnostic); + } + } + + private static bool AnalyzeMemberAssignment(ISimpleAssignmentOperation assignmentOperation, T instance, Func equalityComparer) where T : class, IOperation + { + // Check every simple assignments target in a statement for equality to `instance` + while (assignmentOperation.Value != null && assignmentOperation.Value.Kind == OperationKind.SimpleAssignment) + { + assignmentOperation = (ISimpleAssignmentOperation)assignmentOperation.Value; + + var operationValue = assignmentOperation.Target as T; + if (equalityComparer(instance, operationValue)) + { + return true; + } + } + return false; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf index b6f0b46cec..63769d2163 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf @@ -22,6 +22,16 @@ Některé odkazy na {0} nešlo opravit. Měly by se opravit ručně. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Tam, kde je to vhodné, použijte literály diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf index f83a1173f8..433184e5cd 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf @@ -22,6 +22,16 @@ Einige Verweise auf "{0}" konnten nicht korrigiert werden. Korrigieren Sie sie manuell. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Nach Möglichkeit Literale verwenden diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf index b6098d635e..2c6b34e5d7 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf @@ -22,6 +22,16 @@ Algunas referencias a "{0}" podrían no corregirse, así que tendría que hacerlo manualmente. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Usar literales cuando resulte apropiado diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf index b876656d1b..175f988bce 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf @@ -22,6 +22,16 @@ Des références à '{0}' n'ont pas pu être corrigées, elles doivent être corrigées manuellement. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Utiliser des littéraux quand cela est approprié diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf index e63e15321a..ad6f701c0f 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf @@ -22,6 +22,16 @@ Non è stato possibile correggere alcuni riferimenti a '{0}'. Correggerli manualmente. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Usa valori letterali dove appropriato diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf index 428c32284f..7d4b6d3ff9 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf @@ -22,6 +22,16 @@ '{0}' への参照を修正できませんでした。手動で修正する必要があります。 + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate 適切な場所にリテラルを使用します diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf index 5348c47c54..bc6c9d17e9 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf @@ -22,6 +22,16 @@ '{0}'에 대한 일부 참조를 수정할 수 없습니다. 수동으로 수정해야 합니다. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate 적합한 리터럴을 사용하세요. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf index 89ca2e2d45..fa8796612e 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf @@ -22,6 +22,16 @@ Nie można naprawić niektórych odwołań do „{0}”. Należy je naprawić ręcznie. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Używaj literałów w odpowiednich miejscach diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf index c10c48e8f3..99eb002875 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf @@ -22,6 +22,16 @@ Algumas referências a '{0}' não puderam ser corrigidas, elas devem ser corrigidas manualmente. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Usar literais sempre que apropriado diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf index fbe902cbce..ee0585f23c 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf @@ -22,6 +22,16 @@ Не удалось исправить некоторые ссылки на "{0}". Их следует исправить вручную. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Используйте литералы, когда это уместно diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf index 9b4471ece2..b3f42d10b2 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf @@ -22,6 +22,16 @@ Bazı '{0}' başvuruları düzeltilemedi, bunları kendiniz düzeltmelisiniz. + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate Uygun durumlarda sabit değerler kullanın diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf index 40d74abaf1..c274ddc3b4 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf @@ -22,6 +22,16 @@ 无法修复对“{0}”的部分引用,应手动修复它们。 + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate 在合适的位置使用文本 diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf index c9b22e7b3e..bf70969d41 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf @@ -22,6 +22,16 @@ 無法修正對 '{0}' 的某些參考,應予以手動修正。 + + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + Object '{0}' is being referred to and reassigned in the same statement. You are at risk of referring to unintended object. + + + + Refering to object and reassigning it in the same statement. + Refering to object and reassigning it in the same statement. + + Use literals where appropriate 適當時機使用常值 diff --git a/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatementTests.cs b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatementTests.cs new file mode 100644 index 0000000000..625812a824 --- /dev/null +++ b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/ReferingToObjectAndReassigningItInTheSameStatementTests.cs @@ -0,0 +1,270 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing; +using Microsoft.CodeQuality.Analyzers.QualityGuidelines; +using Test.Utilities; +using Xunit; + +namespace Microsoft.CodeQuality.Analyzers.UnitTests.QualityGuidelines +{ + public partial class ReferingToObjectAndReassigningItInTheSameStatementTests : DiagnosticAnalyzerTestBase + { + protected override DiagnosticAnalyzer GetBasicDiagnosticAnalyzer() + { + return new ReferingToObjectAndReassigningItInTheSameStatement(); + } + + protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() + { + return new ReferingToObjectAndReassigningItInTheSameStatement(); + } + + private DiagnosticResult GetCSharpResultAt(int line, int column, string symbolName) + { + return GetCSharpResultAt(line, column, ReferingToObjectAndReassigningItInTheSameStatement.Rule, symbolName); + } + + private DiagnosticResult GetBasicResultAt(int line, int column, string symbolName) + { + return GetBasicResultAt(line, column, ReferingToObjectAndReassigningItInTheSameStatement.Rule, symbolName); + } + + [Fact] + public void CSharpReassignLocalVariableAndReferToItsField() + { + VerifyCSharp(@" +public class C +{ + public C Field; +} + +public class Test +{ + public void Method() + { + C a, b; + a.Field = a = b; + } +} +", + GetCSharpResultAt(12, 9, "a")); + } + + [Fact] + public void CSharpReassignLocalVariableAndReferToItsProperty() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + +public class Test +{ + public void Method() + { + C a, b, c; + a.Property = c = a = b; + } +} +", + GetCSharpResultAt(12, 9, "a")); + } + + [Fact] + public void CSharpReassignLocalVariablesPropertyAndReferToItsProperty() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + +public class Test +{ + public void Method() + { + C a, b; + a.Property.Property = a.Property = b; + } +} +", + GetCSharpResultAt(12, 9, "Property")); + } + + [Fact] + public void CSharpReassignLocalVariableAndItsPropertyAndReferToItsProperty() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + +public class Test +{ + public void Method() + { + C a, b; + a.Property.Property = a.Property = a = b; + } +} +", + GetCSharpResultAt(12, 9, "Property"), + GetCSharpResultAt(12, 31, "a")); + } + + [Fact] + public void CSharpReferToFieldOfReferenceTypeLocalVariableAfterItsReassignment() + { + VerifyCSharp(@" +public class C +{ + public C Field; +} + +public class Test +{ + static C x, y; + + public void Method() + { + x.Field = x = y; + } +} +", + GetCSharpResultAt(13, 9, "x")); + } + + [Fact] + public void CSharpReassignGlobalVariableAndReferToItsField() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + +public class Test +{ + static C x, y; + + public void Method() + { + x.Property.Property = x.Property = y; + } +} +", + GetCSharpResultAt(13, 9, "Property")); + } + + [Fact] + public void CSharpReassignGlobalVariableAndItsPropertyAndReferToItsProperty() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + +public class Test +{ + static C x, y; + + public void Method() + { + x.Property.Property = x.Property = x = y; + } +} +", + GetCSharpResultAt(13, 9, "Property"), + GetCSharpResultAt(13, 31, "x")); + } + + + [Fact] + public void CSharpReassignGlobalPropertyAndItsPropertyAndReferToItsProperty() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + +public class Test +{ + static C x { get; set; } + static C y { get; set; } + + public void Method() + { + x.Property.Property = x.Property = x = y; + } +} +", + GetCSharpResultAt(14, 9, "Property"), + GetCSharpResultAt(14, 31, "x")); + } + + [Fact] + public void CSharpReassignSecondLocalVariableAndReferToItsPropertyOfFirstVariable() + { + VerifyCSharp(@" +public class C +{ + public C Property { get; set; } +} + +public class Test +{ + public void Method() + { + C a, b; + a.Property = b = a; + } +} +"); + } + + [Fact] + public void CSharpReassignLocalValueTypeVariableAndReferToItsField() + { + VerifyCSharp(@" +public struct S +{ + public S Field; +} + +public class Test +{ + public void Method() + { + S a, b; + a.Field = a = b; + } +} +"); + } + + [Fact] + public void CSharpReassignLocalValueTypeVariableAndReferToItsProperty() + { + VerifyCSharp(@" +public struct S +{ + public S Property { get; set; } +} + +public class Test +{ + public void Method() + { + S a, b; + a.Property = c = a = b; + } +} +"); + } + } +} From d4c9a2124ef101062a1eed796c9234e02b165a2b Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 21 Aug 2019 00:43:53 +0300 Subject: [PATCH 03/16] Rename ReferingToObjectAndReassigningItInTheSameStatement to ReferringToObjectAndReassigningItInTheSameStatement --- ...ftQualityGuidelinesAnalyzersResources.resx | 30 +++++++++---------- ...jectAndReassigningItInTheSameStatement.cs} | 9 +++--- ...QualityGuidelinesAnalyzersResources.cs.xlf | 8 ++--- ...QualityGuidelinesAnalyzersResources.de.xlf | 8 ++--- ...QualityGuidelinesAnalyzersResources.es.xlf | 8 ++--- ...QualityGuidelinesAnalyzersResources.fr.xlf | 8 ++--- ...QualityGuidelinesAnalyzersResources.it.xlf | 8 ++--- ...QualityGuidelinesAnalyzersResources.ja.xlf | 8 ++--- ...QualityGuidelinesAnalyzersResources.ko.xlf | 8 ++--- ...QualityGuidelinesAnalyzersResources.pl.xlf | 8 ++--- ...lityGuidelinesAnalyzersResources.pt-BR.xlf | 8 ++--- ...QualityGuidelinesAnalyzersResources.ru.xlf | 8 ++--- ...QualityGuidelinesAnalyzersResources.tr.xlf | 8 ++--- ...tyGuidelinesAnalyzersResources.zh-Hans.xlf | 8 ++--- ...tyGuidelinesAnalyzersResources.zh-Hant.xlf | 8 ++--- ...ndReassigningItInTheSameStatementTests.cs} | 10 +++---- 16 files changed, 76 insertions(+), 77 deletions(-) rename src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/{ReferingToObjectAndReassigningItInTheSameStatement.cs => ReferringToObjectAndReassigningItInTheSameStatement.cs} (86%) rename src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/{ReferingToObjectAndReassigningItInTheSameStatementTests.cs => ReferringToObjectAndReassigningItInTheSameStatementTests.cs} (91%) diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx index 9fdd120a2f..12e2cd270a 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx @@ -1,17 +1,17 @@ - From 5f94d46edc1494f1224e14b8fffc256e0ea686d2 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 21 Aug 2019 03:06:19 +0300 Subject: [PATCH 08/16] Revert whitespace changes --- ...ftQualityGuidelinesAnalyzersResources.resx | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx index 5f2f5a26f9..d72210ce10 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx @@ -5,9 +5,9 @@ Version 2.0 - The primary goals of this format is to allow a simple XML format - that is mostly human readable. The generation and parsing of the - various data types are done through the TypeConverter classes + The primary goals of this format is to allow a simple XML format + that is mostly human readable. The generation and parsing of the + various data types are done through the TypeConverter classes associated with the data types. Example: @@ -27,35 +27,35 @@ This is a comment - There are any number of "resheader" rows that contain simple + There are any number of "resheader" rows that contain simple name/value pairs. - Each data row contains a name, and value. The row also contains a - type or mimetype. Type corresponds to a .NET class that support - text/value conversion through the TypeConverter architecture. - Classes that don't support this are serialized and stored with the + Each data row contains a name, and value. The row also contains a + type or mimetype. Type corresponds to a .NET class that support + text/value conversion through the TypeConverter architecture. + Classes that don't support this are serialized and stored with the mimetype set. - The mimetype is used for serialized objects, and tells the - ResXResourceReader how to depersist the object. This is currently not + The mimetype is used for serialized objects, and tells the + ResXResourceReader how to depersist the object. This is currently not extensible. For a given mimetype the value must be set accordingly: - Note - application/x-microsoft.net.object.binary.base64 is the format - that the ResXResourceWriter will generate, however the reader can + Note - application/x-microsoft.net.object.binary.base64 is the format + that the ResXResourceWriter will generate, however the reader can read any of the formats listed below. mimetype: application/x-microsoft.net.object.binary.base64 - value : The object must be serialized with + value : The object must be serialized with : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter : and then encoded with base64 encoding. mimetype: application/x-microsoft.net.object.soap.base64 - value : The object must be serialized with + value : The object must be serialized with : System.Runtime.Serialization.Formatters.Soap.SoapFormatter : and then encoded with base64 encoding. mimetype: application/x-microsoft.net.object.bytearray.base64 - value : The object must be serialized into a byte array + value : The object must be serialized into a byte array : using a System.ComponentModel.TypeConverter : and then encoded with base64 encoding. --> From 92a13ab119d04d543e2c4e4412048a7fb652d0bf Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Tue, 3 Sep 2019 21:57:20 +0300 Subject: [PATCH 09/16] Fix Member comparison --- .../MicrosoftQualityGuidelinesAnalyzersResources.resx | 2 +- .../ReferencingAndReassigningObjectInSameStatement.cs | 2 +- .../xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf | 4 ++-- .../xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf | 4 ++-- .../xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf | 4 ++-- .../xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf | 4 ++-- .../xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf | 4 ++-- .../xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf | 4 ++-- .../xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf | 4 ++-- .../xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf | 4 ++-- .../MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf | 4 ++-- .../xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf | 4 ++-- .../xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf | 4 ++-- .../MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf | 4 ++-- .../MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf | 4 ++-- 15 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx index d72210ce10..c78f7e3a65 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx @@ -259,6 +259,6 @@ Referring to object and reassigning it in the same statement. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. \ No newline at end of file diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferencingAndReassigningObjectInSameStatement.cs b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferencingAndReassigningObjectInSameStatement.cs index ad4addb451..0aac8ca3c3 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferencingAndReassigningObjectInSameStatement.cs +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferencingAndReassigningObjectInSameStatement.cs @@ -60,7 +60,7 @@ private void AnalyzeAssignment(OperationAnalysisContext context) } else if (operationTarget.Instance is IMemberReferenceOperation memberInstance) { - isViolationFound = AnalyzeAssignmentToMember(assignmentOperation, memberInstance, (a, b) => a.Member == b.Member); + isViolationFound = AnalyzeAssignmentToMember(assignmentOperation, memberInstance, (a, b) => a.Member == b.Member && a.Instance?.Syntax.ToString() == b.Instance?.Syntax.ToString()); } else if (operationTarget.Instance is IParameterReferenceOperation parameterInstance) { diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf index 0edf6411f1..db5e9dfaf2 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf index dce227055b..5177cd31ad 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf index 39aee3ccfb..733472ae05 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf index e2beb40ab3..bdd4db63ff 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf index 7a85553402..4e55016326 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf index 080cd4dae1..424d1a1b41 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf index 3f47d01aa8..6f65963527 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf index b181c74118..390235ba69 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf index dec2a49def..cc6d92ab6e 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf index 00dbffb898..7a7ea2ed7d 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf index fa7f52ca0a..96db888c9c 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf index 295f7dee2c..8bbf50fe94 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf index 37f490cd84..cb5dbd7751 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf @@ -23,8 +23,8 @@ - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the later assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. From 218558e0840ce6a92bbc2edf290435acdcbd36a9 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Thu, 5 Sep 2019 02:11:56 +0300 Subject: [PATCH 10/16] Update Title according to suggestion Co-Authored-By: Genevieve Warren --- .../MicrosoftQualityGuidelinesAnalyzersResources.resx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx index c78f7e3a65..f3139b2fa6 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx @@ -256,9 +256,9 @@ Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Referring to object and reassigning it in the same statement. + Referencing object and reassigning it in the same statement. When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - \ No newline at end of file + From 9480ecf09be8ec180d42614891ecf31d4cec448b Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Fri, 6 Sep 2019 21:40:21 +0300 Subject: [PATCH 11/16] Changed description and title according to suggestions --- ...gningSymbolAndItsMemberInSameStatement.cs} | 8 ++--- ...ftQualityGuidelinesAnalyzersResources.resx | 12 ++++---- ...QualityGuidelinesAnalyzersResources.cs.xlf | 30 +++++++++---------- ...QualityGuidelinesAnalyzersResources.de.xlf | 30 +++++++++---------- ...QualityGuidelinesAnalyzersResources.es.xlf | 30 +++++++++---------- ...QualityGuidelinesAnalyzersResources.fr.xlf | 30 +++++++++---------- ...QualityGuidelinesAnalyzersResources.it.xlf | 30 +++++++++---------- ...QualityGuidelinesAnalyzersResources.ja.xlf | 30 +++++++++---------- ...QualityGuidelinesAnalyzersResources.ko.xlf | 30 +++++++++---------- ...QualityGuidelinesAnalyzersResources.pl.xlf | 30 +++++++++---------- ...lityGuidelinesAnalyzersResources.pt-BR.xlf | 30 +++++++++---------- ...QualityGuidelinesAnalyzersResources.ru.xlf | 30 +++++++++---------- ...QualityGuidelinesAnalyzersResources.tr.xlf | 30 +++++++++---------- ...tyGuidelinesAnalyzersResources.zh-Hans.xlf | 30 +++++++++---------- ...tyGuidelinesAnalyzersResources.zh-Hant.xlf | 30 +++++++++---------- ...SymbolAndItsMemberInSameStatementTests.cs} | 10 +++---- 16 files changed, 210 insertions(+), 210 deletions(-) rename src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/{ReferencingAndReassigningObjectInSameStatement.cs => AssigningSymbolAndItsMemberInSameStatement.cs} (87%) rename src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/{ReferencingAndReassigningObjectInSameStatementTests.cs => AssigningSymbolAndItsMemberInSameStatementTests.cs} (92%) diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferencingAndReassigningObjectInSameStatement.cs b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs similarity index 87% rename from src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferencingAndReassigningObjectInSameStatement.cs rename to src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs index 0aac8ca3c3..7f5fbe7e25 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/ReferencingAndReassigningObjectInSameStatement.cs +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs @@ -13,13 +13,13 @@ namespace Microsoft.CodeQuality.Analyzers.QualityGuidelines /// CA2246: Prevent objects from being referenced in statements where they are reassigned /// [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] - public sealed class ReferencingAndReassigningObjectInSameStatement : DiagnosticAnalyzer + public sealed class AssigningSymbolAndItsMemberInSameStatement : DiagnosticAnalyzer { internal const string RuleId = "CA2246"; - private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.ReferencingAndReassigningObjectInSameStatementTitle), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); - private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.ReferencingAndReassigningObjectInSameStatementMessage), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); - private static readonly LocalizableString s_localizableDescription = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.ReferencingAndReassigningObjectInSameStatementDescription), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); + private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.AssigningSymbolAndItsMemberInSameStatementTitle), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); + private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.AssigningSymbolAndItsMemberInSameStatementMessage), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); + private static readonly LocalizableString s_localizableDescription = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.AssigningSymbolAndItsMemberInSameStatementDescription), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(RuleId, s_localizableTitle, diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx index f3139b2fa6..eec04cb4ee 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/MicrosoftQualityGuidelinesAnalyzersResources.resx @@ -252,13 +252,13 @@ The property {0} should not be assigned to itself. - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. - - Referencing object and reassigning it in the same statement. + + Assigning symbol and its member in the same statement. - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf index db5e9dfaf2..dde8448950 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.cs.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. Vlastnost {0} nesmí být přiřazena sama k sobě. @@ -22,21 +37,6 @@ Některé odkazy na {0} nešlo opravit. Měly by se opravit ručně. - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate Tam, kde je to vhodné, použijte literály diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf index 5177cd31ad..d42afc9309 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.de.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. Die Eigenschaft "{0}" darf nicht sich selbst zugewiesen werden. @@ -22,21 +37,6 @@ Einige Verweise auf "{0}" konnten nicht korrigiert werden. Korrigieren Sie sie manuell. - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate Nach Möglichkeit Literale verwenden diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf index 733472ae05..9078a5150e 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.es.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. La propiedad {0} no debe asignarse a sí misma. @@ -22,21 +37,6 @@ Algunas referencias a "{0}" podrían no corregirse, así que tendría que hacerlo manualmente. - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate Usar literales cuando resulte apropiado diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf index bdd4db63ff..ce73890dcc 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.fr.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. La propriété {0} ne doit pas être assignée à elle-même. @@ -22,21 +37,6 @@ Des références à '{0}' n'ont pas pu être corrigées, elles doivent être corrigées manuellement. - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate Utiliser des littéraux quand cela est approprié diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf index 4e55016326..5c970e2154 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.it.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. La proprietà {0} non deve essere assegnata a se stessa. @@ -22,21 +37,6 @@ Non è stato possibile correggere alcuni riferimenti a '{0}'. Correggerli manualmente. - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate Usa valori letterali dove appropriato diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf index 424d1a1b41..238e58c90e 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ja.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. プロパティ {0} をそれ自体に割り当てることはできません。 @@ -22,21 +37,6 @@ '{0}' への参照を修正できませんでした。手動で修正する必要があります。 - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate 適切な場所にリテラルを使用します diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf index 6f65963527..786c9064e2 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ko.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. {0} 속성을 자체에 할당하면 안 됩니다. @@ -22,21 +37,6 @@ '{0}'에 대한 일부 참조를 수정할 수 없습니다. 수동으로 수정해야 합니다. - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate 적합한 리터럴을 사용하세요. diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf index 390235ba69..6c4293ce74 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pl.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. Właściwość {0} nie powinna być przypisana do samej siebie. @@ -22,21 +37,6 @@ Nie można naprawić niektórych odwołań do „{0}”. Należy je naprawić ręcznie. - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate Używaj literałów w odpowiednich miejscach diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf index cc6d92ab6e..3d8d7f0094 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.pt-BR.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. A propriedade {0} não deve ser atribuída a si mesmo. @@ -22,21 +37,6 @@ Algumas referências a '{0}' não puderam ser corrigidas, elas devem ser corrigidas manualmente. - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate Usar literais sempre que apropriado diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf index 7a7ea2ed7d..ef8fe0b9dd 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.ru.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. Свойство {0} нельзя назначать самому себе. @@ -22,21 +37,6 @@ Не удалось исправить некоторые ссылки на "{0}". Их следует исправить вручную. - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate Используйте литералы, когда это уместно diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf index 96db888c9c..8c3ba4cbc6 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.tr.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. {0} özelliği kendisine atanmamalıdır. @@ -22,21 +37,6 @@ Bazı '{0}' başvuruları düzeltilemedi, bunları kendiniz düzeltmelisiniz. - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate Uygun durumlarda sabit değerler kullanın diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf index 8bbf50fe94..87fce6e8b9 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hans.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. 不得将属性 {0} 分配给其自身。 @@ -22,21 +37,6 @@ 无法修复对“{0}”的部分引用,应手动修复它们。 - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate 在合适的位置使用文本 diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf index cb5dbd7751..418699af26 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/xlf/MicrosoftQualityGuidelinesAnalyzersResources.zh-Hant.xlf @@ -2,6 +2,21 @@ + + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + Assigning to a symbol and its member (field/property) in the same statement is not recommended. It is not clear if the member access was intended to use symbol's old value prior to the assignment or new value from the assignment in this statement. For clarity, consider splitting the assignments into separate statements. + + + + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + Symbol '{0}' and its member '{1}' are both assigned in the same statement. You are at risk of assigning the member of an unintended object. + + + + Assigning symbol and its member in the same statement. + Assigning symbol and its member in the same statement. + + The property {0} should not be assigned to itself. 不應將屬性 {0} 指派給屬性自身。 @@ -22,21 +37,6 @@ 無法修正對 '{0}' 的某些參考,應予以手動修正。 - - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - When you go through the assignment chain right to left, firstly, reference in object '{0}' will be changed, but when you reffer to this object second time, since it's still the same statement, it will be pointing to it's old value. So if old value of '{0}' isn't saved elsewhere, result of the latter assignment will be lost. - - - - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - Object '{0}' is referenced and reassigned in the same statement. You are at risk of referring to unintended object. - - - - Referring to object and reassigning it in the same statement. - Referring to object and reassigning it in the same statement. - - Use literals where appropriate 適當時機使用常值 diff --git a/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/ReferencingAndReassigningObjectInSameStatementTests.cs b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs similarity index 92% rename from src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/ReferencingAndReassigningObjectInSameStatementTests.cs rename to src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs index 1aa7c2e57a..23ededcb89 100644 --- a/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/ReferencingAndReassigningObjectInSameStatementTests.cs +++ b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs @@ -8,26 +8,26 @@ namespace Microsoft.CodeQuality.Analyzers.UnitTests.QualityGuidelines { - public partial class ReferencingAndReassigningObjectInSameStatementTests : DiagnosticAnalyzerTestBase + public partial class AssigningSymbolAndItsMemberInSameStatementTests : DiagnosticAnalyzerTestBase { protected override DiagnosticAnalyzer GetBasicDiagnosticAnalyzer() { - return new ReferencingAndReassigningObjectInSameStatement(); + return new AssigningSymbolAndItsMemberInSameStatement(); } protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() { - return new ReferencingAndReassigningObjectInSameStatement(); + return new AssigningSymbolAndItsMemberInSameStatement(); } private DiagnosticResult GetCSharpResultAt(int line, int column, string symbolName) { - return GetCSharpResultAt(line, column, ReferencingAndReassigningObjectInSameStatement.Rule, symbolName); + return GetCSharpResultAt(line, column, AssigningSymbolAndItsMemberInSameStatement.Rule, symbolName); } private DiagnosticResult GetBasicResultAt(int line, int column, string symbolName) { - return GetBasicResultAt(line, column, ReferencingAndReassigningObjectInSameStatement.Rule, symbolName); + return GetBasicResultAt(line, column, AssigningSymbolAndItsMemberInSameStatement.Rule, symbolName); } [Fact] From 8c9bd829ff5ebe7191252c8e435a160b877ae96f Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sat, 7 Sep 2019 12:26:27 +0300 Subject: [PATCH 12/16] Fixed unit tests --- ...igningSymbolAndItsMemberInSameStatement.cs | 2 +- ...gSymbolAndItsMemberInSameStatementTests.cs | 24 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs index 7f5fbe7e25..526d93b1a2 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs @@ -73,7 +73,7 @@ private void AnalyzeAssignment(OperationAnalysisContext context) if (isViolationFound) { - var diagnostic = Diagnostic.Create(Rule, operationTarget.Syntax.GetLocation(), operationTarget.Instance.Syntax.ToString()); + var diagnostic = Diagnostic.Create(Rule, operationTarget.Syntax.GetLocation(), operationTarget.Instance.Syntax, operationTarget.Member.Name); context.ReportDiagnostic(diagnostic); } } diff --git a/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs index 23ededcb89..c5ce807d1f 100644 --- a/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs +++ b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs @@ -48,7 +48,7 @@ public void Method() } } ", - GetCSharpResultAt(12, 9, "a")); + GetCSharpResultAt(12, 9, "a", "Field")); } [Fact] @@ -69,7 +69,7 @@ public void Method() } } ", - GetCSharpResultAt(12, 9, "a")); + GetCSharpResultAt(12, 9, "a", "Property")); } [Fact] @@ -90,7 +90,7 @@ public void Method() } } ", - GetCSharpResultAt(12, 9, "a.Property")); + GetCSharpResultAt(12, 9, "a.Property", "Property")); } [Fact] @@ -111,8 +111,8 @@ public void Method() } } ", - GetCSharpResultAt(12, 9, "a.Property"), - GetCSharpResultAt(12, 31, "a")); + GetCSharpResultAt(12, 9, "a.Property", "Property"), + GetCSharpResultAt(12, 31, "a", "Property")); } [Fact] @@ -134,7 +134,7 @@ public void Method() } } ", - GetCSharpResultAt(13, 9, "x")); + GetCSharpResultAt(13, 9, "x", "Field")); } [Fact] @@ -156,7 +156,7 @@ public void Method() } } ", - GetCSharpResultAt(13, 9, "x.Property")); + GetCSharpResultAt(13, 9, "x.Property", "Property")); } [Fact] @@ -178,8 +178,8 @@ public void Method() } } ", - GetCSharpResultAt(13, 9, "x.Property"), - GetCSharpResultAt(13, 31, "x")); + GetCSharpResultAt(13, 9, "x.Property", "Property"), + GetCSharpResultAt(13, 31, "x", "Property")); } @@ -203,8 +203,8 @@ public void Method() } } ", - GetCSharpResultAt(14, 9, "x.Property"), - GetCSharpResultAt(14, 31, "x")); + GetCSharpResultAt(14, 9, "x.Property", "Property"), + GetCSharpResultAt(14, 31, "x", "Property")); } [Fact] @@ -285,7 +285,7 @@ public void Method(C b) } } ", - GetCSharpResultAt(12, 9, "b")); + GetCSharpResultAt(12, 9, "b", "Property")); } [Fact] From e89b2c2084e2cde9a3333d999c0774739860847d Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Wed, 11 Sep 2019 23:53:56 +0300 Subject: [PATCH 13/16] Fix errors, symbol comparison, enable concurrent execution, configure generated code analysis --- .../AssigningSymbolAndItsMemberInSameStatement.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs index 526d93b1a2..f96153b277 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs @@ -17,9 +17,9 @@ public sealed class AssigningSymbolAndItsMemberInSameStatement : DiagnosticAnaly { internal const string RuleId = "CA2246"; - private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.AssigningSymbolAndItsMemberInSameStatementTitle), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); - private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.AssigningSymbolAndItsMemberInSameStatementMessage), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); - private static readonly LocalizableString s_localizableDescription = new LocalizableResourceString(nameof(MicrosoftQualityGuidelinesAnalyzersResources.AssigningSymbolAndItsMemberInSameStatementDescription), MicrosoftQualityGuidelinesAnalyzersResources.ResourceManager, typeof(MicrosoftQualityGuidelinesAnalyzersResources)); + private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(MicrosoftCodeQualityAnalyzersResources.AssigningSymbolAndItsMemberInSameStatementTitle), MicrosoftCodeQualityAnalyzersResources.ResourceManager, typeof(MicrosoftCodeQualityAnalyzersResources)); + private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString(nameof(MicrosoftCodeQualityAnalyzersResources.AssigningSymbolAndItsMemberInSameStatementMessage), MicrosoftCodeQualityAnalyzersResources.ResourceManager, typeof(MicrosoftCodeQualityAnalyzersResources)); + private static readonly LocalizableString s_localizableDescription = new LocalizableResourceString(nameof(MicrosoftCodeQualityAnalyzersResources.AssigningSymbolAndItsMemberInSameStatementDescription), MicrosoftCodeQualityAnalyzersResources.ResourceManager, typeof(MicrosoftCodeQualityAnalyzersResources)); internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(RuleId, s_localizableTitle, @@ -33,6 +33,9 @@ public sealed class AssigningSymbolAndItsMemberInSameStatement : DiagnosticAnaly public override void Initialize(AnalysisContext context) { + context.EnableConcurrentExecution(); + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); + context.RegisterOperationAction(AnalyzeAssignment, OperationKind.SimpleAssignment); } @@ -56,15 +59,15 @@ private void AnalyzeAssignment(OperationAnalysisContext context) bool isViolationFound = false; if (operationTarget.Instance is ILocalReferenceOperation localInstance) { - isViolationFound = AnalyzeAssignmentToMember(assignmentOperation, localInstance, (a, b) => a.Local == b.Local); + isViolationFound = AnalyzeAssignmentToMember(assignmentOperation, localInstance, (a, b) => a.Local.Equals(b.Local)); } else if (operationTarget.Instance is IMemberReferenceOperation memberInstance) { - isViolationFound = AnalyzeAssignmentToMember(assignmentOperation, memberInstance, (a, b) => a.Member == b.Member && a.Instance?.Syntax.ToString() == b.Instance?.Syntax.ToString()); + isViolationFound = AnalyzeAssignmentToMember(assignmentOperation, memberInstance, (a, b) => a.Member.Equals(b.Member) && a.Instance?.Syntax.ToString() == b.Instance?.Syntax.ToString()); } else if (operationTarget.Instance is IParameterReferenceOperation parameterInstance) { - isViolationFound = AnalyzeAssignmentToMember(assignmentOperation, parameterInstance, (a, b) => a.Parameter == b.Parameter); + isViolationFound = AnalyzeAssignmentToMember(assignmentOperation, parameterInstance, (a, b) => a.Parameter.Equals(b.Parameter)); } else { From 8a8f1e9c3edcc425760e854f1a3d238b2a57e8bd Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Thu, 12 Sep 2019 00:07:48 +0300 Subject: [PATCH 14/16] Fix tests --- .../AssigningSymbolAndItsMemberInSameStatementTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs index c5ce807d1f..e7e69daf74 100644 --- a/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs +++ b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs @@ -230,7 +230,7 @@ public void Method() [Fact] public void CSharpReassignPropertyOfFirstLocalVariableWithSecondAndReferToPropertyOfSecondVariable() { - VerifyCSharpDiagnostic(@" + VerifyCSharp(@" public class C { public C Property { get; set; } @@ -250,7 +250,7 @@ public void Method() [Fact] public void CSharpReassignPropertyOfFirstLocalVariableWithThirdAndReferToPropertyOfSecondVariable() { - VerifyCSharpDiagnostic(@" + VerifyCSharp(@" public class C { public C Property { get; set; } @@ -270,7 +270,7 @@ public void Method() [Fact] public void CSharpReassignMethodParameterAndReferToItsProperty() { - VerifyCSharpDiagnostic(@" + VerifyCSharp(@" public class C { public C Property { get; set; } From 8c43b7ee20792d95056a3e843f4846babdc9e513 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Thu, 12 Sep 2019 01:47:22 +0300 Subject: [PATCH 15/16] Fix test runtime errors --- ...gSymbolAndItsMemberInSameStatementTests.cs | 55 ++++++++----------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs index e7e69daf74..e9dfdf21a1 100644 --- a/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs +++ b/src/Microsoft.CodeQuality.Analyzers/UnitTests/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatementTests.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Testing; using Microsoft.CodeQuality.Analyzers.QualityGuidelines; using Test.Utilities; using Xunit; @@ -20,16 +19,6 @@ protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() return new AssigningSymbolAndItsMemberInSameStatement(); } - private DiagnosticResult GetCSharpResultAt(int line, int column, string symbolName) - { - return GetCSharpResultAt(line, column, AssigningSymbolAndItsMemberInSameStatement.Rule, symbolName); - } - - private DiagnosticResult GetBasicResultAt(int line, int column, string symbolName) - { - return GetBasicResultAt(line, column, AssigningSymbolAndItsMemberInSameStatement.Rule, symbolName); - } - [Fact] public void CSharpReassignLocalVariableAndReferToItsField() { @@ -43,12 +32,12 @@ public class Test { public void Method() { - C a, b; + C a = new C(), b = new C(); a.Field = a = b; } } ", - GetCSharpResultAt(12, 9, "a", "Field")); + GetCSharpResultAt(12, 9, AssigningSymbolAndItsMemberInSameStatement.Rule, "a", "Field")); } [Fact] @@ -64,12 +53,12 @@ public class Test { public void Method() { - C a, b, c; + C a = new C(), b = new C(), c; a.Property = c = a = b; } } ", - GetCSharpResultAt(12, 9, "a", "Property")); + GetCSharpResultAt(12, 9, AssigningSymbolAndItsMemberInSameStatement.Rule, "a", "Property")); } [Fact] @@ -85,12 +74,12 @@ public class Test { public void Method() { - C a, b; + C a = new C(), b = new C(); a.Property.Property = a.Property = b; } } ", - GetCSharpResultAt(12, 9, "a.Property", "Property")); + GetCSharpResultAt(12, 9, AssigningSymbolAndItsMemberInSameStatement.Rule, "a.Property", "Property")); } [Fact] @@ -106,13 +95,13 @@ public class Test { public void Method() { - C a, b; + C a = new C(), b = new C(); a.Property.Property = a.Property = a = b; } } ", - GetCSharpResultAt(12, 9, "a.Property", "Property"), - GetCSharpResultAt(12, 31, "a", "Property")); + GetCSharpResultAt(12, 9, AssigningSymbolAndItsMemberInSameStatement.Rule, "a.Property", "Property"), + GetCSharpResultAt(12, 31, AssigningSymbolAndItsMemberInSameStatement.Rule, "a", "Property")); } [Fact] @@ -134,7 +123,7 @@ public void Method() } } ", - GetCSharpResultAt(13, 9, "x", "Field")); + GetCSharpResultAt(13, 9, AssigningSymbolAndItsMemberInSameStatement.Rule, "x", "Field")); } [Fact] @@ -156,7 +145,7 @@ public void Method() } } ", - GetCSharpResultAt(13, 9, "x.Property", "Property")); + GetCSharpResultAt(13, 9, AssigningSymbolAndItsMemberInSameStatement.Rule, "x.Property", "Property")); } [Fact] @@ -178,8 +167,8 @@ public void Method() } } ", - GetCSharpResultAt(13, 9, "x.Property", "Property"), - GetCSharpResultAt(13, 31, "x", "Property")); + GetCSharpResultAt(13, 9, AssigningSymbolAndItsMemberInSameStatement.Rule, "x.Property", "Property"), + GetCSharpResultAt(13, 31, AssigningSymbolAndItsMemberInSameStatement.Rule, "x", "Property")); } @@ -203,8 +192,8 @@ public void Method() } } ", - GetCSharpResultAt(14, 9, "x.Property", "Property"), - GetCSharpResultAt(14, 31, "x", "Property")); + GetCSharpResultAt(14, 9, AssigningSymbolAndItsMemberInSameStatement.Rule, "x.Property", "Property"), + GetCSharpResultAt(14, 31, AssigningSymbolAndItsMemberInSameStatement.Rule, "x", "Property")); } [Fact] @@ -220,7 +209,7 @@ public class Test { public void Method() { - C a, b; + C a = new C(), b; a.Property = b = a; } } @@ -240,7 +229,7 @@ public class Test { public void Method() { - C a, b, c; + C a = new C(), b = new C(), c; b.Property.Property = a.Property = b; } } @@ -260,7 +249,7 @@ public class Test { public void Method() { - C a, b, c; + C a = new C(), b = new C(), c = new C(); b.Property.Property = a.Property = c; } } @@ -280,12 +269,12 @@ public class Test { public void Method(C b) { - C a; + C a = new C(); b.Property = b = a; } } ", - GetCSharpResultAt(12, 9, "b", "Property")); + GetCSharpResultAt(12, 9, AssigningSymbolAndItsMemberInSameStatement.Rule, "b", "Property")); } [Fact] @@ -305,7 +294,7 @@ public void Method() a.Field = a = b; } } -"); +", TestValidationMode.AllowCompileErrors); } [Fact] @@ -325,7 +314,7 @@ public void Method() a.Property = c = a = b; } } -"); +", TestValidationMode.AllowCompileErrors); } } } From 057ecb98f99ff1342f31b97e9b28ab1843cf4232 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Thu, 12 Sep 2019 12:16:09 +0300 Subject: [PATCH 16/16] Make analyzer C# only --- .../AssigningSymbolAndItsMemberInSameStatement.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs index f96153b277..aa73798712 100644 --- a/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs +++ b/src/Microsoft.CodeQuality.Analyzers/Core/QualityGuidelines/AssigningSymbolAndItsMemberInSameStatement.cs @@ -12,7 +12,7 @@ namespace Microsoft.CodeQuality.Analyzers.QualityGuidelines /// /// CA2246: Prevent objects from being referenced in statements where they are reassigned /// - [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] + [DiagnosticAnalyzer(LanguageNames.CSharp)] public sealed class AssigningSymbolAndItsMemberInSameStatement : DiagnosticAnalyzer { internal const string RuleId = "CA2246"; @@ -97,4 +97,4 @@ private void AnalyzeAssignment(OperationAnalysisContext context) return false; } } -} \ No newline at end of file +}