From 4dc4707dc1d7556dbeb8684b44f7e656223cff5a Mon Sep 17 00:00:00 2001 From: Carlos dos Santos Date: Mon, 5 Sep 2016 13:56:11 -0300 Subject: [PATCH] Fix #774 - ignore pointer declaration --- .../MakeLocalVariableConstWhenItIsPossibleAnalyzer.cs | 3 +++ .../MakeLocalVariablesConstWhenItIsPossibleTests.cs | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/CSharp/CodeCracker/Performance/MakeLocalVariableConstWhenItIsPossibleAnalyzer.cs b/src/CSharp/CodeCracker/Performance/MakeLocalVariableConstWhenItIsPossibleAnalyzer.cs index f5c8e2b29..869a15ed4 100644 --- a/src/CSharp/CodeCracker/Performance/MakeLocalVariableConstWhenItIsPossibleAnalyzer.cs +++ b/src/CSharp/CodeCracker/Performance/MakeLocalVariableConstWhenItIsPossibleAnalyzer.cs @@ -36,6 +36,8 @@ private static void AnalyzeNode(SyntaxNodeAnalysisContext context) var localDeclaration = (LocalDeclarationStatementSyntax)context.Node; var semanticModel = context.SemanticModel; + if (localDeclaration.GetType().IsPointer) return; + if (!localDeclaration.IsConst && IsDeclarationConstFriendly(localDeclaration, semanticModel) && AreVariablesOnlyWrittenInsideDeclaration(localDeclaration, semanticModel) ) @@ -61,6 +63,7 @@ static bool IsDeclarationConstFriendly(LocalDeclarationStatementSyntax declarati // if reference type, value is null? var variableTypeName = declaration.Declaration.Type; var variableType = semanticModel.GetTypeInfo(variableTypeName).ConvertedType; + if (variableType.TypeKind == TypeKind.Pointer) return false; if (variableType.IsReferenceType && variableType.SpecialType != SpecialType.System_String && constantValue.Value != null) return false; // nullable? diff --git a/test/CSharp/CodeCracker.Test/Performance/MakeLocalVariablesConstWhenItIsPossibleTests.cs b/test/CSharp/CodeCracker.Test/Performance/MakeLocalVariablesConstWhenItIsPossibleTests.cs index 1fc75b5c0..8cfa21591 100644 --- a/test/CSharp/CodeCracker.Test/Performance/MakeLocalVariablesConstWhenItIsPossibleTests.cs +++ b/test/CSharp/CodeCracker.Test/Performance/MakeLocalVariablesConstWhenItIsPossibleTests.cs @@ -52,6 +52,13 @@ public async Task IgnoresVariablesThatChangesValueOutsideDeclaration() await VerifyCSharpHasNoDiagnosticsAsync(test); } + public async Task IgnoresPointerDeclarations() + { + var test = @"void* value = null;".WrapInCSharpMethod(); + + await VerifyCSharpHasNoDiagnosticsAsync(test); + } + [Fact] public async Task CreateDiagnosticsWhenAssigningAPotentialConstant() {