Skip to content

Commit

Permalink
Merge pull request #73391 from CyrusNajmabadi/usePatternFix
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored May 8, 2024
2 parents c0a3973 + 522b66a commit b8b6a0d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ private static bool IsTopmostExpression(ExpressionSyntax node)
{
return node.WalkUpParentheses().Parent switch
{
LambdaExpressionSyntax _ => true,
AssignmentExpressionSyntax _ => true,
ConditionalExpressionSyntax _ => true,
ExpressionSyntax _ => false,
LambdaExpressionSyntax => true,
AssignmentExpressionSyntax => true,
ConditionalExpressionSyntax => true,
ExpressionSyntax => false,
_ => true
};
}
Expand All @@ -142,10 +142,10 @@ private static bool IsTrivial(AnalyzedPattern pattern)
{
return pattern switch
{
Not { Pattern: Constant _ } => true,
Not { Pattern: Source { PatternSyntax: ConstantPatternSyntax _ } } => true,
Not _ => false,
Binary _ => false,
Not { Pattern: Constant } => true,
Not { Pattern: Source { PatternSyntax: ConstantPatternSyntax } } => true,
Not => false,
Binary => false,
_ => true
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal static async Task<DebugDataTipInfo> GetInfoAsync(Document document, int
: default;
}

if (expression.IsAnyLiteralExpression())
if (expression is LiteralExpressionSyntax)
{
// If the user hovers over a literal, give them a DataTip for the type of the
// literal they're hovering over.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public async Task TestMissingOnExpression(string expression)
await TestAllMissingOnExpressionAsync(expression);
}

[InlineData("i == default || i > default(int)", "i is default(int) or > (default(int))")]
[InlineData("i == default || i > default(int)", "i is default(int) or > default(int)")]
[InlineData("!(o is C c)", "o is not C c")]
[InlineData("o is int ii && o is long jj", "o is int ii and long jj")]
[InlineData("o is string || o is Exception", "o is string or Exception")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static bool IsTypeApparentInAssignmentExpression(
}

// literals, use var if options allow usage here.
if (initializerExpression.IsAnyLiteralExpression())
if (initializerExpression is LiteralExpressionSyntax)
{
return stylePreferences.HasFlag(UseVarPreference.ForBuiltInTypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,6 @@ private static bool AddSimpleName(SimpleNameSyntax simpleName, List<string> part
return true;
}

public static bool IsAnyLiteralExpression(this ExpressionSyntax expression)
=> expression is LiteralExpressionSyntax;

public static bool IsInConstantContext([NotNullWhen(true)] this ExpressionSyntax? expression)
{
if (expression == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,15 @@ public static bool CanRemoveParentheses(
// (null) -> null
// (default) -> default;
// (1) -> 1
if (expression.IsAnyLiteralExpression())
if (expression is LiteralExpressionSyntax)
return true;

// (typeof(int)) -> typeof(int)
// (default(int)) -> default(int)
// (checked(1)) -> checked(1)
// (unchecked(1)) -> unchecked(1)
// (sizeof(int)) -> sizeof(int)
if (expression is TypeOfExpressionSyntax or DefaultExpressionSyntax or CheckedExpressionSyntax or SizeOfExpressionSyntax)
return true;

// (this) -> this
Expand Down

0 comments on commit b8b6a0d

Please sign in to comment.