From 2232a639364901360a32943395543683b3111bcd Mon Sep 17 00:00:00 2001 From: mkonicek Date: Tue, 7 Dec 2010 19:10:04 +0100 Subject: [PATCH] Context actions - Fix: "Check for (not) null" is only offered for expressions where it makes sense (method call, property access, variable access, "as" typecast). --- .../ContextActions/CheckAssignmentCache.cs | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/CheckAssignmentCache.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/CheckAssignmentCache.cs index 24d4b755fe..f0a3aaaa33 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/CheckAssignmentCache.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/CheckAssignmentCache.cs @@ -72,8 +72,8 @@ namespace SharpRefactoring.ContextActions var identifier = assignment.Left as IdentifierExpression; if (identifier == null) return null; - if (assignment.Right is ObjectCreateExpression) - // // don't offer action for "a = new Foo()" + if (!ExpressionCanBeNull(assignment.Right)) + // don't offer action where it makes no sense return null; return identifier.Identifier; } @@ -85,11 +85,22 @@ namespace SharpRefactoring.ContextActions if (declaration.Variables.Count != 1) return null; VariableDeclaration varDecl = declaration.Variables[0]; - if (!varDecl.Initializer.IsNull && - // don't offer action for "var a = new Foo()" - !(varDecl.Initializer is ObjectCreateExpression)) - return varDecl.Name; - return null; + if (!ExpressionCanBeNull(varDecl.Initializer)) + // don't offer action where it makes no sense + return null; + return varDecl.Name; + } + + bool ExpressionCanBeNull(Expression expr) + { + if (expr == null) return false; + if (expr.IsNull) return false; + if (expr is PrimitiveExpression) return false; + if (expr is IdentifierExpression) return true; + if (expr is MemberReferenceExpression) return true; + if (expr is InvocationExpression) return true; + if (expr is CastExpression && ((CastExpression)expr).CastType == CastType.TryCast) return true; + return false; } CodeGenerator GetCodeGenerator(EditorContext context)