-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
"When variable type is apparent" doesn't apply to array initialization #26531
Comments
tag @jcouv as you originally fixed this. |
This is by design. See #24114. |
Specifically, the preference for explicit types on built-in types dominates the preference for |
The design is broken: #23714. |
@davkean The design may not match your expectations, but we have no intention of making a universally-configurable system that meets all possible expectations (attempting to do so would doom the entire system to failing for everyone under an impossible testing burden). Some expectations will be broken for each user, but the majority styles are accommodated to a degree deemed reasonably possible. |
"When type is apparent" is one of the options but it doesn't work. |
Do you have a suggestion for different wording that would more accurately reflect the behavior? For example:
Alternately, is there a way to present the three options such that it's clear the set of three items is ordered? |
The current set of options is:
I believe having these options instead would make sense and only increase the number o possible combinations from 8 to 9 while solving this issue:
|
Yes that makes sense. I'm trying to understand under what circumstances someone would choose the following (which is effectively what options are enforcing now):
I've never seen that style anywhere. Whereas the style I want enforced both project-system and CoreFx/CoreCLR enforce. |
Unfortunately my proposal can't be done in a backwards compatible way because it removes 2 combinations that are currently possible. I created a table mapping current options to the proposed ones:
It would no longer be possible to prefer explicit type when type is apparent but var when it is not apparent. This does seem like a bizarre preference, but even if it was OK to remove this, I don't know how it could be done with editrconfig to map these. On the other hand, this proposal would add 3 new possible preferences: built_in_types elsewhere
1. when_type_is_apparent never
2. when_type_is_apparent always
3. when_type_is_apparent when_type_is_apparent where number 3 is the desired one, 2 seems somewhat reasonable and 1 does not. |
I don't think so. I would expect |
At least it was my understanding that "apparent" means "the type is explicitly spelled out inside the expression". Maybe I'm wrong. |
Reopening to see if the proposal from @Neme12 could provide a suitable replacement for current behavior without being overwhelming. |
@Neme12 The analysis provided comparing the two sets of options is good, but currently misses some of the behaviors. I'm specifically interested in understanding the following cases:
The current options can represent "allow when type is apparent, and disallow otherwise". I'm concerned the new options would subtly change that to "require when type is apparent, and disallow otherwise". |
Observed behavior indicates that this seems to only trigger for built in types. With the following config:
Array initialization of a built in type triggers IDE0008 . var buffer = new byte[n]; However custom types do not trigger the error. var buffer = new MyCustomType[n]; |
I reviewed #26531 (comment) and #26531 (comment) again today with @mavasani. I believe my original concerns can now be addressed by setting the severity for the diagnostic ID instead of setting it as part of the code style configuration. For example, the "allow when apparent, disallow otherwise" setting could be: csharp_style_var_for_built_in_types = never
csharp_style_var_elsewhere = when_type_is_apparent
# IDE0007: Use implicit type
dotnet_diagnostic.IDE0007.severity = hidden
# IDE0008: Use explicit type
dotnet_diagnostic.IDE0008.severity = warning |
This the same bug I filed #23907, but it stated that it was fixed.
Version Used:
Version 15.8.0 Preview 1.0 [27628.0.d15.8]
Steps to Reproduce:
Expected Behavior:
No message
Actual Behavior:
Message IDE0008 Use explicit type instead of 'var' ConsoleApp300 C:\Users\davkean\Source\Repos\ConsoleApp300\ConsoleApp300\Program.cs 6 Active
The text was updated successfully, but these errors were encountered: