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

Port rule CA1011 - Consider passing base types as parameters #61916

Open
Evangelink opened this issue Nov 22, 2021 · 4 comments
Open

Port rule CA1011 - Consider passing base types as parameters #61916

Evangelink opened this issue Nov 22, 2021 · 4 comments
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-Meta code-analyzer Marks an issue that suggests a Roslyn analyzer
Milestone

Comments

@Evangelink
Copy link
Member

Evangelink commented Nov 22, 2021

VS rule description: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1011?view=vs-2022&redirectedfrom=MSDN&viewFallbackFrom=vs-2015

I think we should file a separate issue on dotnet\runtime and allow the runtime team to triage the correct severity/enabled by default state of this ported FxCop rule. @buyaa-n @carlossanlop

Originally posted by @mavasani in dotnet/roslyn-analyzers#5356 (comment)

If I get the green light from you, I will address the few comments made by @mavasani and will wait for your input in term of severity and enabled by default state.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Nov 22, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@buyaa-n buyaa-n changed the title Port rule CA1011 - roslyn-analyzers Port rule CA1011 - Consider passing base types as parameters Nov 22, 2021
@buyaa-n buyaa-n added the code-analyzer Marks an issue that suggests a Roslyn analyzer label Nov 22, 2021
@carlossanlop carlossanlop added this to the Future milestone Nov 23, 2021
@carlossanlop
Copy link
Member

carlossanlop commented Nov 23, 2021

The usage is pretty straightforward.

The original issue is here.

There is already a PR porting this fixer from FxCop, and it's blocked by having this proposal approved (or not).

Existing MS Docs for CA1011: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1011?view=vs-2022

  • Suggested category: Design (same as FxCop)
  • Suggested severity: Hidden, because it's currently labeled as an analyzer/fixer that can cause a breaking change, and could potentially be noisy.
  • Suggested milestone: Future

Flag

        // Before
        public void ReadNextByte(FileStream stream)
        {
            while (stream.ReadByte() != -1) { /*...*/ }
        }

        // After
        public void ReadNextByte(Stream anyStream)
        {
            while (anyStream.ReadByte() != -1) { /*...*/ }
        }

Do not flag

        // Before
        public void ReadHandle(FileStream stream)
        {
            while (anyStream.ReadByte() != -1) { /*...*/ } // ReadByte is available in Stream
            var handle = stream.SafeFileHandle; // But SafeFileHandle is only available in FileStream
        }

(Maybe) do not flag

The alternative to "not flagging" is to give the suggestion anyway, but let the user supress it / ignore it

interface IResource
{
    string Location { get; }
}

class FileResource : IResource
{
    // Location represents a file path
}
class HttpResource : IResource
{
    // Location represents an HTTP url
}

// Should not be corrected to IResource
string GetContent(FileResource res)
{
    return File.ReadAllText(res.Location); // the type encodes information about what the object represents
}

@carlossanlop carlossanlop added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed untriaged New issue has not been triaged by the area owner labels Nov 23, 2021
@bartonjs
Copy link
Member

bartonjs commented Nov 30, 2021

Video

  • Generally looks good as proposed, but we changed Hidden to IdeSuggestion

Category: Design
Severity: IdeSuggestion

@bartonjs bartonjs added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Nov 30, 2021
@carlossanlop
Copy link
Member

PR implementing this: dotnet/roslyn-analyzers#5356

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved in API review, it can be implemented area-Meta code-analyzer Marks an issue that suggests a Roslyn analyzer
Projects
None yet
Development

No branches or pull requests

4 participants