Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentNullException.ThrowIfNull is no side effect #463

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,51 @@ public static void SetText(DependencyObject element, string? value)
RoslynAssert.Valid(Analyzer, code);
}


[Test]
public static void AttachedPropertyWithNewNullChecks()
{
var code = @"
namespace N
{
using System;
using System.Windows;

public static class WithNullChecks
{
public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
""Text"",
typeof(string),
typeof(WithNullChecks),
new PropertyMetadata(default(string)));

/// <summary>Helper for setting <see cref=""TextProperty""/> on <paramref name=""element""/>.</summary>
/// <param name=""element""><see cref=""DependencyObject""/> to set <see cref=""TextProperty""/> on.</param>
/// <param name=""value"">Text property value.</param>
public static void SetText(DependencyObject element, string? value)
{
ArgumentNullException.ThrowIfNull(element);

element.SetValue(TextProperty, value);
}

/// <summary>Helper for getting <see cref=""TextProperty""/> from <paramref name=""element""/>.</summary>
/// <param name=""element""><see cref=""DependencyObject""/> to read <see cref=""TextProperty""/> from.</param>
/// <returns>Text property value.</returns>
[AttachedPropertyBrowsableForType(typeof(DependencyObject))]
public static string? GetText(DependencyObject element)
{
ArgumentNullException.ThrowIfNull(element);

return (string?)element.GetValue(TextProperty);
}
}
}";

RoslynAssert.Valid(Analyzer, code);
}


[Test]
public static void AttachedPropertyWithEmptyIfs()
{
Expand Down
7 changes: 7 additions & 0 deletions WpfAnalyzers/Analyzers/ClrMethodDeclarationAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ private static bool TryGetSideEffect(BlockSyntax body, InvocationExpressionSynta
{
switch (statement)
{
case ExpressionStatementSyntax { Expression: { } expression }
when NullCheck.IsNullCheck(expression, null, CancellationToken.None, out _):
continue;
case ExpressionStatementSyntax { Expression: InvocationExpressionSyntax { Expression: MemberAccessExpressionSyntax { Expression: IdentifierNameSyntax { } name } expression } }
when name.ToString() == nameof(System.ArgumentNullException) &&
expression.Name.ToString() == "ThrowIfNull":
continue;
case ExpressionStatementSyntax { Expression: { } expression }
when expression == getOrSet:
continue;
Expand Down
Loading