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

Usage analysis ignores references in documentation comments #2773

Closed
sharwell opened this issue May 14, 2015 · 47 comments
Closed

Usage analysis ignores references in documentation comments #2773

sharwell opened this issue May 14, 2015 · 47 comments
Assignees
Labels
Area-Compilers Bug Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Milestone

Comments

@sharwell
Copy link
Member

Migrated from CodePlex

Consider the following class.

namespace SampleNamespace
{
    using System;
    using System.Threading.Tasks;

    /// <summary>
    /// Some stuff is at
    /// <see cref="Task.ContinueWith(Action{Task, object}, object)"/>.
    /// </summary>
    public class SampleClass
    {
    }
}

The behavior of Organize Usings varies by whether or not the current project is configured to generate an XML documentation file with the output.

  • If XML documentation file is enabled: The Organize Usings feature will not make any changes to the file shown above.
  • If XML documentation file is disabled: The Organize Usings feature will remove both of the using declarations in the file shown above.

At least for the purpose of analyzing using declarations, the behavior of Roslyn should not depend on whether or not the project is currently configured to produce an XML documentation file during the build.

@srivatsn
Copy link
Contributor

The "unused using" diagnostic is emitted by the compiler. It looks like the compiler is not even looking at doc comments when it doesn't need to generate a xml doc file and so doesn't include that code in it's analysis.

@Pilchie
Copy link
Member

Pilchie commented May 15, 2015

We have an option to "Diagnose" xml doc comments but not emit them, but that also triggers all the other warnings and errors, and so I don't think VS integration sets it.

We might need some finer grained control over the compiler's handling of xml doc comments. I remember discussing this with @amcasey a LONG time ago, but I don't think we resolved anything.

@amcasey
Copy link
Member

amcasey commented May 15, 2015

@matkoch
Copy link

matkoch commented Jun 10, 2015

+1

@Pilchie
Copy link
Member

Pilchie commented Jul 31, 2015

Hitcount++ from internal bug 1167079

@jnm2
Copy link
Contributor

jnm2 commented Jan 1, 2017

If removing the using breaks a cref, it should not be suggested. Please fix.

Currently this results in bouncing back and forth between ReSharper complaining that you should add the using for the XML documentation and Roslyn complaining that you should remove it. It's a bad user experience.

If the only correct solution is truly to enable XML doc generation, then please stop suggesting removal of the using and start suggesting enabling XML documentation output.

This may be a moonshot, but in an ideal world, I think I should be able to leave XML documentation output off and still have all the syntax checking as though it was on, minus the warning about publicly visible types missing comments.

@jaredpar jaredpar modified the milestones: 3.0, 2.1 Feb 7, 2017
@sharwell
Copy link
Member Author

@svick are you able to test this on tunnelvisionlabs/dotnet-threading?

@breyed
Copy link

breyed commented Mar 23, 2018

FWIW in thinking about the tradeoffs, the workflow I generally use is to configure debug builds to be as fast as possible, so I leave XML comments off. I rely on a release build, with all diagnostics, including static code analysis and XML comments, to be run often enough to catch deeper problems in a timely fashion.

It's OK for the diagnostic build to be slower. However, it's not OK when I see that the usings in a C# file have gotten jumbled and so press Ctrl+R, Ctrl+G (Remove and Sort Warnings), to subtly introduce warnings or errors into the next diagnostic build.

@gafter gafter self-assigned this Apr 22, 2018
@gafter gafter added 3 - Working and removed Developer Community The issue was originally reported on https://developercommunity.visualstudio.com help wanted The issue is "up for grabs" - add a comment if you are interested in working on it labels Apr 25, 2018
@gafter gafter modified the milestones: 16.0, 15.8 Apr 25, 2018
@gafter
Copy link
Member

gafter commented Apr 26, 2018

@dotnet/roslyn-ide @dotnet/roslyn-analysis @Pilchie @DustinCampbell I'm working on this issue now. My proposal is that the compiler will not produce any "unused using" diagnostics when the syntax trees are parsed with DocumentationMode.None, because in that case we have no way of knowing if they were used or not. I believe the IDE uses DocumentationMode.Parse, which gives the compiler enough information to determine if the usings are used or not, so this should not detract from the IDE experience. The IDE experience after my fix would be that we now properly recognize that usings (imports for VB) are used in doc comments.

Are you OK with this?

@jcouv
Copy link
Member

jcouv commented Apr 26, 2018

@gafter That seems a worse experience, in my mind.
There was some analysis (above) of parsing the documentation in all cases (but without emitting extra files). It looked like the performance hit was minimal. Why not do that?

gafter added a commit to gafter/roslyn that referenced this issue Apr 26, 2018
Also silently process doc comments when parsing them so we can report unused usings.
Fixes dotnet#2773
@gafter gafter added 4 - In Review A fix for the issue is submitted for review. and removed 3 - Working labels Apr 26, 2018
@jnm2
Copy link
Contributor

jnm2 commented Apr 26, 2018

I think the same way as @jcouv.

@gafter
Copy link
Member

gafter commented Apr 27, 2018

There was some analysis (above) of parsing the documentation in all cases (but without emitting extra files). It looked like the performance hit was minimal. Why not do that?

Because we get a different syntax tree, and that is a compat break for an unknown number of clients. Are you suggesting that we Obsolete DocumentationMode.None, which parses doc comments as ordinary comments?

@mavasani
Copy link
Contributor

mavasani commented Apr 27, 2018

I am +1 on @gafter's proposal as it retains the existing API and parse semantics for all documentation comment modes, does not increase the command line build time (however negligible) and improves the user experience for IDE scenario.

API and parse semantics are very explicit on the DocumentationMode enum:

    public enum DocumentationMode : byte
    {
        /// <summary>
        /// Treats documentation comments as regular comments.
        /// </summary>
        None = 0,
 
        /// <summary>
        /// Parses documentation comments as structured trivia, but do not report any diagnostics.
        /// </summary>
        Parse = 1,
 
        /// <summary>
        /// Parses documentation comments as structured trivia and report diagnostics.
        /// </summary>
        Diagnose = 2,
    }

None should parse doc comments as regular comments, which means unused using diagnostics can never be produced reliably. IDE does not use this mode and command line compilation would have never output these diagnostics as they have hidden severity. This is also likely the most commonly used mode in command line compilation, and I am not sure if it is worth risking any performance hit here by trying to parse documentation comments.

Parse should parse documentation comments, and not report any documentation comment diagnostics. We should fix this mode to correctly consume documentation comments for determining unused usings. I don't think this can realistically break any external API consumers, as we not going to produce any new diagnostics, just stop producing incorrect unused using diagnostics. Hence, I don't think we need any new documentation mode such as Bind.

@gafter gafter removed the 4 - In Review A fix for the issue is submitted for review. label May 3, 2018
gafter added a commit that referenced this issue May 3, 2018
…26425)

* Do not report unused using/imports if not processing doc comments.
Also silently process doc comments when parsing them so we can report unused usings.
Fixes #2773
@gafter
Copy link
Member

gafter commented May 3, 2018

Fixed in #26425

@Pilchie
Copy link
Member

Pilchie commented May 3, 2018

Hurray! Finally! Thanks @gafter!

@sharwell sharwell added the Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented label May 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Projects
None yet
Development

No branches or pull requests