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

Use [CallerArgumentExpression] for parameterName argument in Check methods #33524

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

stevendarby
Copy link
Contributor

  • Use [CallerArgumentExpression] for parameterName argument in Check methods
  • Remove explicit setting of parameterName from uses of these methods

Fixes #33523

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

Steven.Darby added 2 commits April 12, 2024 17:37
…methods

- Remove explicit setting of parameterName from uses of these methods

Fixes dotnet#33523
Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - good code quality improvements, but see comments/suggestions below.

BTW we should also probably do this to Check.DebugAssert as well, making the 2nd argument optional; for places where we want a custom message, that can still of course be passed, but it's nice to not have to specify it and get the actual argument expression for free (/cc @ajcvickers)

{
if (value is null)
{
NotEmpty(parameterName, nameof(parameterName));
NotEmpty(parameterName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm mistaken, with [CallerArgumentExpression] the argument can never be empty (i.e. it's guaranteed to be populated by the compiler)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's populated by the compiler if it's not explicitly set, but it can still be explicitly set, so this check may be worthwhile against future misuse. But I'm happy to remove it if you're sure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right; though passing a null here would go against the nullable annotations for that parameter, so I don't think we need to check for that (or have a special check for the empty string...). So yeah, let's remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed all NotEmpty checks on parameterName

src/Shared/Check.cs Show resolved Hide resolved
src/Shared/Check.cs Show resolved Hide resolved
@@ -10,7 +10,7 @@ public class CheckTest
[ConditionalFact]
public void Not_null_throws_when_arg_is_null()
// ReSharper disable once NotResolvedInText
=> Assert.Throws<ArgumentNullException>(() => Check.NotNull<string>(null, "foo"));
=> Assert.Throws<ArgumentNullException>(() => Check.NotNull<string>(null));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just for completeness, these tests could also check that the right message is thrown

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests which check the right ParamName is supplied. Noticed the ParamName was not supplied for some, so fixed this. Note that HasNoNulls was using the parameterName as the message. Have not added tests for messages too but can if really needed. Using ConditionalTheory even for single test case so it is actually checking an argument.

@roji
Copy link
Member

roji commented Apr 21, 2024

@stevendarby just a reminder to request a new review (upper-right corner) when you're done with modifications.

@roji
Copy link
Member

roji commented Apr 29, 2024

@stevendarby just a ping to make sure you haven't forgotten about this.

@stevendarby
Copy link
Contributor Author

@roji sorry for the delay, had some build issues last time I tried to address the comments- I'd just updated to the latest VS preview, not sure if related. Will have another go this evening and post back either way, but if you want to jump in before then please feel free!

@stevendarby stevendarby requested a review from roji April 30, 2024 10:23
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.

Use [CallerArgumentExpression] in Check helper methods
2 participants