Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 2, 2025

  • Understand the current MA0015 analyzer implementation
  • Extend analyzer to detect static Throw methods on ArgumentException and ArgumentNullException
  • Add tests for the new functionality
  • Update documentation
  • Fix code formatting
  • Run code review
  • Run security checks
  • Use CompilationStart to query types once (optimization)
  • Validate the paramName parameter when explicitly provided as second argument
  • Add tests for paramName validation
  • Refactor validation logic into separate methods for better code readability
  • Check CallerArgumentExpressionAttribute to find paramName parameter instead of assuming it's the second argument
  • Add null safety checks for AttributeClass and Parameter
  • Add support for ArgumentOutOfRangeException ThrowIf methods
  • Add tests for expression scenarios (null, boolean expressions)
  • Optimize registration to only register invocation action when CallerArgumentExpressionAttribute is available
  • Use IsEqualToAny for type checking
  • Move method name check to top for performance
  • Use GetAttribute instead of foreach loop
  • Rename ValidateFirstArgument to ValidateExpression and make it dynamic
  • Report error for expressions that don't match parameters
  • Add test for ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual with diagnostic
  • Remove unnecessary op parameter and simplify ValidateExpression logic

Changes in this commit:

  • Removed op parameter from ValidateExpression - now uses GetParameterNames(argument, ...) directly
  • Added UnwrapImplicitConversionOperations() to handle implicit conversions before checking operation type
  • Simplified logic: IParameterReferenceOperation is always valid (no validation needed)
  • Only validate member references against available parameter names
  • Clearer separation of concerns in the validation logic
Original prompt

This section details on the original issue you should resolve

<issue_title>MA0015 is not reported for static Throw methods on Argument(Null)Exception</issue_title>
<issue_description>### Version of the Meziantou.Analyzer NuGet package

2.0.235

Rule Identifier

MAA0015

Target Framework

.NET 9.0

C# Language version

4.14.0-3.25412.6 (c167adef)

Description

MA0015 is not reported for Argument(Null)Exception.ThrowIfNull*(...) while it's correctly reported if you use the constructors of Argument(Null)Exception.

Reproduction Steps

using System;

internal sealed class Program
{
    private static void Main(string[] args)
    {
        if (Name is null)
        {
            throw new ArgumentNullException(nameof(Name)); // OK
        }

        ArgumentNullException.ThrowIfNull(Name);  // MA0015 is not reported
        ArgumentNullException.ThrowIfNullOrEmpty(Name);  // MA0015 is not reported
        ArgumentNullException.ThrowIfNullOrWhiteSpace(Name); // MA0015 is not reported

        ArgumentException.ThrowIfNullOrWhiteSpace(Name); // MA0015 is not reported
        ArgumentException.ThrowIfNullOrEmpty(Name); // MA0015 is not reported
    }

    public static string Name { get; }
}

Other information

No response</issue_description>

Comments on the Issue (you are @copilot in this section)

@meziantou I agree the rule could be improved to support the new Throw methods. I'll mark it as `Help wanted` as I may not have time to work on it soon.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix MA0015 not reported for static Throw methods Fix MA0015 not detecting static ThrowIf methods on ArgumentException Nov 2, 2025
Copilot AI requested a review from meziantou November 2, 2025 01:34
Copilot finished work on behalf of meziantou November 2, 2025 01:34
@meziantou
Copy link
Owner

@copilot

  • Can you use CompilationStart to query types?
  • If ArgumentNullException.ThrowIfNull() has 2 arguments and the second one is not null, check the second one (string)

Copy link
Contributor Author

Copilot AI commented Nov 2, 2025

@copilot

  • Can you use CompilationStart to query types?
  • If ArgumentNullException.ThrowIfNull() has 2 arguments and the second one is not null, check the second one (string)

Done in commits 7251bb3 and d911bd2:

  1. ✅ Using RegisterCompilationStartAction to query types once at compilation start
  2. ✅ Validating the second argument (paramName) when it's explicitly provided and not null

The analyzer now:

  • Queries ArgumentException and ArgumentNullException types once at compilation start for better performance
  • When a second argument (paramName) is provided to ThrowIf methods, validates that string parameter instead of inferring from the first argument
  • Suggests using nameof operator when paramName is a valid parameter but passed as a string literal
  • Refactored validation logic into separate methods (ValidateParamNameArgument and ValidateFirstArgument) for better readability

Copilot finished work on behalf of meziantou November 2, 2025 02:16
Copilot AI requested a review from meziantou November 2, 2025 03:05
Copilot finished work on behalf of meziantou November 2, 2025 03:05
Copilot AI changed the title Fix MA0015 not detecting static ThrowIf methods on ArgumentException Fix MA0015 not detecting static ThrowIf methods on ArgumentException and related types Nov 2, 2025
Copilot AI requested a review from meziantou November 2, 2025 03:16
IhateTrains pushed a commit to ParadoxGameConverters/ImperatorToCK3 that referenced this pull request Nov 3, 2025
Updated
[Meziantou.Analyzer](https://github.com/meziantou/Meziantou.Analyzer)
from 2.0.239 to 2.0.248.

<details>
<summary>Release notes</summary>

_Sourced from [Meziantou.Analyzer's
releases](https://github.com/meziantou/Meziantou.Analyzer/releases)._

## 2.0.248

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/2.0.248>

## What's Changed
* Update MA0053 to mention both class and record types by @​Copilot in
meziantou/Meziantou.Analyzer#915


**Full Changelog**:
meziantou/Meziantou.Analyzer@2.0.247...2.0.248

## 2.0.247

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/2.0.247>

## What's Changed
* Update Roslyn dependencies and clean up PackageReferences by
@​meziantou in meziantou/Meziantou.Analyzer#916


**Full Changelog**:
meziantou/Meziantou.Analyzer@2.0.246...2.0.247

## 2.0.246

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/2.0.246>

**Full Changelog**:
meziantou/Meziantou.Analyzer@2.0.245...2.0.246

## 2.0.245

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/2.0.245>

## What's Changed
* Fix MA0015 not detecting static ThrowIf methods on ArgumentException
and related types by @​Copilot in
meziantou/Meziantou.Analyzer#912


**Full Changelog**:
meziantou/Meziantou.Analyzer@2.0.244...2.0.245

## 2.0.244

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/2.0.244>

## What's Changed
* Fix MA0011 not reported for types with ToString(IFormatProvider) but
no IFormattable by @​Copilot in
meziantou/Meziantou.Analyzer#910


**Full Changelog**:
meziantou/Meziantou.Analyzer@2.0.243...2.0.244

## 2.0.243

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/2.0.243>

## What's Changed
* Add Roslyn analyzer for multiline XML comments by @​Copilot in
meziantou/Meziantou.Analyzer#908

## New Contributors
* @​Copilot made their first contribution in
meziantou/Meziantou.Analyzer#908

**Full Changelog**:
meziantou/Meziantou.Analyzer@2.0.242...2.0.243

## 2.0.242

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/2.0.242>

**Full Changelog**:
meziantou/Meziantou.Analyzer@2.0.241...2.0.242

## 2.0.241

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/2.0.241>

**Full Changelog**:
meziantou/Meziantou.Analyzer@2.0.240...2.0.241

## 2.0.240

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/2.0.240>

## What's Changed
* Add copilot instructions by @​meziantou in
meziantou/Meziantou.Analyzer#907


**Full Changelog**:
meziantou/Meziantou.Analyzer@2.0.239...2.0.240

Commits viewable in [compare
view](meziantou/Meziantou.Analyzer@2.0.239...2.0.248).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=Meziantou.Analyzer&package-manager=nuget&previous-version=2.0.239&new-version=2.0.248)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This was referenced Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MA0015 is not reported for static Throw methods on Argument(Null)Exception

2 participants