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

Fix S3900 FN: Conversion operations #7002

Merged
merged 10 commits into from
Mar 29, 2023
Prev Previous commit
Next Next commit
Review: add more test cases
  • Loading branch information
zsolt-kolbay-sonarsource committed Mar 29, 2023
commit 73030fd537d678085b96bbfd8baec07f13b091f7
Original file line number Diff line number Diff line change
Expand Up @@ -638,49 +638,63 @@ public class Conversion
{
public void DownCast(object o)
{
((string)o).ToString(); // Noncompliant
((string)o).ToString(); // Noncompliant
}

public void UpCast(string s)
{
((object)s).ToString(); // Noncompliant
((object)s).ToString(); // Noncompliant
}

public void CastWithPropertyAccess(object o)
{
_ = ((Exception)o).Message; // Noncompliant
_ = ((Exception)o).Message; // Noncompliant
}

public void CastWithRedundantParentheses(object o)
{
(((string)o)).ToString(); // Noncompliant
(((string)o)).ToString(); // Noncompliant
}

public void MultipleCasts(object o)
{
((string)((object)((string)o))).ToString(); // Noncompliant
((string)((object)((string)o))).ToString(); // Noncompliant
}

public void AsOperatorDownCast(object o)
{
(o as string).ToString(); // Noncompliant
(o as string).ToString(); // Noncompliant
}

public void AsOperatorUpCast(string s)
{
(s as object).ToString(); // Noncompliant
(s as object).ToString(); // Noncompliant
}

public void ForEachLoop(object[] arr)
{
foreach (object o in arr) // Noncompliant - the array is first cast to an IEnumerable, then the GetEnumerator method is invoked on it
foreach (object o in arr) // Noncompliant - the array is first cast to an IEnumerable, then the GetEnumerator method is invoked on it
{
}
}

public void ForEachLoopWithCast(object[] arr)
{
foreach (object o in (IEnumerable<object>)arr) // Noncompliant
foreach (object o in (IEnumerable<object>)arr) // Noncompliant
{
}
}

public void ForEachLoopWithParentheses(object[] arr)
{
foreach (object o in ((arr))) // Noncompliant
{
}
}

public void ForEachLoopWithCastAndParentheses(object[] arr)
{
foreach (object o in ((object[])(object)((arr)))) // Noncompliant
{
}
}
Expand Down