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

Fix invalid parameter definitions when methods have nonzero enum default values #68

Conversation

simonmckenzie
Copy link
Contributor

This addresses a bug where there's no cast when generating a definition for a parameter with a nonzero default enum parameter value. Previously, it would generate values like this:

void MethodWithDefaultParameter(MyEnum value = 1);

This would not compile, as there's no implicit conversion from nonzero integral types to enum values. The only implicit enum conversion is from zero (§10.2.4), which the new code does handle.

This was reported in issue #67.

Changes

@simonmckenzie simonmckenzie force-pushed the fix/handle-enum-default-values branch from 3c06186 to 9f6c968 Compare January 27, 2025 01:15
…ult values

This addresses a bug where there's no cast when generating a definition for a parameter with a nonzero default enum parameter value. Previously, it would generate values like this:

```csharp
void MethodWithDefaultParameter(MyEnum value = 1);
```

This would not compile, as there's no implicit conversion from nonzero integral types to enum values.

- Reinstated the default value generation logic that was removed in codecentric#65, as default values generated by Roslyn aren't guaranteed to be valid code, and, in the case of enum default values, clearly aren't.
- Added special case when generating default values for enum parameters (cast nonzero values to the enum type)
- Added tests, and moved all parameter tests into the `.MethodParameters.cs` test file
@simonmckenzie simonmckenzie force-pushed the fix/handle-enum-default-values branch from 9f6c968 to e125699 Compare January 27, 2025 01:17
Comment on lines +138 to +146
string value => $"\"{value}\"",
bool value => $"{(value ? "true" : "false")}",
object value when IsEnum(parameterSymbol.Type, out var enumType) && !Equals(value, 0)
=> $"({enumType!.ToDisplayString(FullyQualifiedDisplayFormat)}){value}",
// struct
null when parameterSymbol.Type.IsValueType
=> $"default({parameterSymbol.Type.ToDisplayString(FullyQualifiedDisplayFormat)})",
null => "null",
_ => $"{parameterSymbol.ExplicitDefaultValue}",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code, apart from the enum-specific branch, has been restored as-is from the version of the class prior to the last PR.

@simonmckenzie simonmckenzie changed the title Fix invalid parameter definitions when methods have nonzero enum defalt values Fix invalid parameter definitions when methods have nonzero enum default values Jan 27, 2025
@simonmckenzie
Copy link
Contributor Author

Superseded by #70. It solves the problem much more simply!

@simonmckenzie simonmckenzie deleted the fix/handle-enum-default-values branch January 27, 2025 09:34
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.

1 participant