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

Field used in blazor component shown as "value assigned but never used" #20137

Closed
vsfeedback opened this issue Mar 25, 2020 · 12 comments
Closed
Assignees
Labels
affected-most This issue impacts most of the customers area-blazor Includes: Blazor, Razor Components area-razor.compiler This issue is related to the Razor compiler (now external) Author: Migration Bot 🤖 The issue was created by a issue mover bot. The author may not be the actual author. bug This issue describes a behavior which is not expected - a bug. feature-razor.language severity-major This label is used by an internal tool
Milestone

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


I have a blazor project with nullable enabled. Here I get loads of warnings for fields who VS thinks are assigned but never used. But when I search for references (see attachement) you can see that they are used in the blazor component and that VS actuall does figure out that this is a reference to the field.


Original Comments

Visual Studio Feedback System on 3/11/2020, 08:26 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Taylor Mullen [MSFT] on 3/18/2020, 11:09 AM:

Hello, would you mind providing a simple repro project so we can investigate further?

Nino Padrutt on 3/18/2020, 02:25 PM:

Hi Taylor

I just wanted to create one, but I noticed it that with the 16.5 the issue is resolved.

Thanks
Nino

Taylor Mullen [MSFT] on 3/18/2020, 02:29 PM:

Ah, good to hear. I'll mark this appropriately.

Nino Padrutt on 3/18/2020, 02:37 PM:

(private comment, text removed)

Nino Padrutt on 3/25/2020, 04:33 AM:

Hi Taylor
I saw that this issue is still marked as resolved. Seeing my last comment and the example project this is so far not the case.
Thanks
Nino


Original Solutions

Visual Studio Feedback System solved on 3/18/2020, 02:29 PM, 0 votes:

A fix for this issue has been released! Install the most recent release from https://visualstudio.microsoft.com/downloads/. Thank you for providing valuable feedback which has helped improve the product.

@ghost ghost added the Author: Migration Bot 🤖 The issue was created by a issue mover bot. The author may not be the actual author. label Mar 25, 2020
@NTaylorMullen NTaylorMullen added area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. labels Mar 25, 2020
@NTaylorMullen
Copy link
Contributor

NTaylorMullen commented Mar 25, 2020

Repro steps:

  1. Create a new Blazor project (doesn't matter what type)
  2. Create a counter partial class:
public partial class Counter
{
    private bool showSomething = false;
}
  1. Add the following to Counter.razor:
@if (showSomething)
{
    <p>Hello World</p>
}
  1. Build the project. You'll get:
1>------ Build started: Project: BlazorApp_CustomerRepro1, Configuration: Debug Any CPU ------
1>C:\Users\nimullen\source\repos\BlazorApp_CustomerRepro1\BlazorApp_CustomerRepro1\Pages\Counter.razor.cs(10,22,10,35): warning CS0414: The field 'Counter.showSomething' is assigned but its value is never used
1>BlazorApp_CustomerRepro1 -> C:\Users\nimullen\source\repos\BlazorApp_CustomerRepro1\BlazorApp_CustomerRepro1\bin\Debug\netcoreapp3.1\BlazorApp_CustomerRepro1.dll
1>BlazorApp_CustomerRepro1 -> C:\Users\nimullen\source\repos\BlazorApp_CustomerRepro1\BlazorApp_CustomerRepro1\bin\Debug\netcoreapp3.1\BlazorApp_CustomerRepro1.Views.dll
1>Done building project "BlazorApp_CustomerRepro1.csproj".
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


This looks to be an issue with how we generate the skeletons of our Blazor files in our pre-build step because that pre-build step removes all Razor content other than what's in @functions / @code. This would technically be fixed by source generators but those are on the cutline for .NET 5.

@mkArtakMSFT
Copy link
Member

@NTaylorMullen why is step #2 (set Nullability) required here?

@mkArtakMSFT mkArtakMSFT added this to the Backlog milestone Mar 26, 2020
@NTaylorMullen
Copy link
Contributor

@NTaylorMullen why is step #2 (set Nullability) required here?

Copy and paste error. It isn't.

@yudielcurbelo
Copy link

This has not been resolved. If you declare a string variable in the code behind (.razor.cs) and assign it in the .razor file you will get the warning.

@celalergun
Copy link

For those who suffers from this strange phenomenon will please take a look at: https://stackoverflow.com/questions/61427520/separate-code-file-in-blazor-gives-bogus-warning-messages

@mikewodarczyk
Copy link

This should be fixed, but when I saw in one of the associated issues a suggestion to go back to using inheritance instead of partial classes, I decided to take a half step and just make these properties "protected" instead of "private". The compiler doesn't complain about protected properties not being read because it doesn't know what you will do with them in a child class. This workaround isn't a solution though because it takes away the needed check for variables that you declare and actually never use.

@ClassyCircuit
Copy link

When is this going to be fixed? It's annoying to see those warnings from analyzers.

@NPadrutt
Copy link

NPadrutt commented Aug 3, 2020

Another work around is to make fields into properties. At least for me for properties the reference is calculated correctly.

@captainsafia captainsafia added affected-most This issue impacts most of the customers severity-major This label is used by an internal tool labels Oct 6, 2020
@ghost
Copy link

ghost commented Oct 9, 2020

Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@ghost
Copy link

ghost commented Mar 11, 2021

Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@pranavkm
Copy link
Contributor

Resolved as part of the source generator work.

@captainsafia
Copy link
Member

@pranavkm Last I tried this, it still repros even with source generators in place. I think there's still some work that we need to do here.

See https://github.com/captainsafia/TestPartialClassAssignment.

@captainsafia captainsafia reopened this May 19, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affected-most This issue impacts most of the customers area-blazor Includes: Blazor, Razor Components area-razor.compiler This issue is related to the Razor compiler (now external) Author: Migration Bot 🤖 The issue was created by a issue mover bot. The author may not be the actual author. bug This issue describes a behavior which is not expected - a bug. feature-razor.language severity-major This label is used by an internal tool
Projects
None yet
Development

No branches or pull requests