diff --git a/ICSharpCode.NRefactory.Tests/CSharp/InsertParenthesesVisitorTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/InsertParenthesesVisitorTests.cs index 604ec79bcf..1415ca3f38 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/InsertParenthesesVisitorTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/InsertParenthesesVisitorTests.cs @@ -181,6 +181,27 @@ namespace ICSharpCode.NRefactory.CSharp "select a)", InsertReadable(expr)); } + [Test] + public void QueryInTypeTest() + { + Expression expr = new QueryExpression { + Clauses = new QueryClause[] { + new QueryFromClause { + Identifier = "a", + Expression = new IdentifierExpression("b") + }, + new QuerySelectClause { + Expression = new IdentifierExpression("a") + } + } + }.IsType(new PrimitiveType("int")); + + Assert.AreEqual("(from a in b" + Environment.NewLine + + "select a) is int", InsertRequired(expr)); + Assert.AreEqual("(from a in b" + Environment.NewLine + + "select a) is int", InsertReadable(expr)); + } + [Test] public void PrePost() { diff --git a/ICSharpCode.NRefactory/CSharp/OutputVisitor/InsertParenthesesVisitor.cs b/ICSharpCode.NRefactory/CSharp/OutputVisitor/InsertParenthesesVisitor.cs index 76d6580a03..abdd06e630 100644 --- a/ICSharpCode.NRefactory/CSharp/OutputVisitor/InsertParenthesesVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/OutputVisitor/InsertParenthesesVisitor.cs @@ -257,11 +257,13 @@ namespace ICSharpCode.NRefactory.CSharp { // Query expressions are strange beasts: // "var a = -from b in c select d;" is valid, so queries bind stricter than unary expressions. - // However, the end of the query is greedy. So their start sort of have a high precedence, + // However, the end of the query is greedy. So their start sort of has a high precedence, // while their end has a very low precedence. We handle this by checking whether a query is used // as left part of a binary operator, and parenthesize it if required. if (queryExpression.Role == BinaryOperatorExpression.LeftRole) Parenthesize(queryExpression); + if (queryExpression.Parent is IsExpression || queryExpression.Parent is AsExpression) + Parenthesize(queryExpression); if (InsertParenthesesForReadability) { // when readability is desired, always parenthesize query expressions within unary or binary operators if (queryExpression.Parent is UnaryOperatorExpression || queryExpression.Parent is BinaryOperatorExpression)