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

"Substring can be simplified" misses basic cases #45637

Closed
davkean opened this issue Jul 3, 2020 · 3 comments · Fixed by #45693
Closed

"Substring can be simplified" misses basic cases #45637

davkean opened this issue Jul 3, 2020 · 3 comments · Fixed by #45693
Labels
Area-IDE Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Milestone

Comments

@davkean
Copy link
Member

davkean commented Jul 3, 2020

Version Used:
IDE0057 handles morecomplicated cases of Substring, but not the basic cases.

Steps to Reproduce:

var value = "Hello World"
value = value.Substring(1);

Expected Behavior:
A analyzer message, asking me to change above to:

var value = "Hello World"
value = value[1...];

Actual Behavior:
Nothing

@davkean davkean changed the title Substring can be simplified misses basic cases "Substring can be simplified" misses basic cases Jul 3, 2020
@CyrusNajmabadi
Copy link
Member

Agreed. We can special case this specific member to work in this fashion. Do you know of other apis that have the same semantics? (i.e. they expose a Foo(int start) method that is equiv to Foo(int start, Length)?

@CyrusNajmabadi CyrusNajmabadi added Area-IDE help wanted The issue is "up for grabs" - add a comment if you are interested in working on it labels Jul 3, 2020
@CyrusNajmabadi
Copy link
Member

Adding help-wanted. We would take a PR for this.

@AdamSpeight2008
Copy link
Contributor

It would be cool if when could express "simplifications", via attributes so we could apply transformations without knowing internals of the method. The compiler apply these transforms at compile-time, a bit like constant-folding (turned to 11).

/// <transformations>
///  <applicable_transformation>
///    <if_requirements_met>
///      <IsArgConstant(nameof(source)) IsNonNull="true" />
///      <IsArgConstant(nameof(index) ) />
///      <IsArgConstant(nameof(count) ) />
///    </if_requirements_met>
///    <then_consider>
///      <result(typeof(char))>
///         <arg(nameof(source)).Index(0)>
///      </result>
///    </then_consider>
///  </applicable_transformation>
/// </transformation>
public String Substring(String source, Int32 index, Int32 count)
{
}

@vatsalyaagrawal vatsalyaagrawal modified the milestones: 16.8, Next Jul 7, 2020
@vatsalyaagrawal vatsalyaagrawal added the Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented label Jul 7, 2020
@jinujoseph jinujoseph removed the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label Jul 7, 2020
@JoeRobich JoeRobich modified the milestones: Next, 16.8.P1 Jul 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants