Skip to content

Merge v0.1.0 into main #51

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

Closed
wants to merge 20 commits into from
Closed

Merge v0.1.0 into main #51

wants to merge 20 commits into from

Conversation

rjmurillo
Copy link
Owner

No description provided.

rjmurillo and others added 14 commits August 1, 2024 23:39
Add [SquiggleCop](https://github.com/MattKotsenas/SquiggleCop) to
prevent unintended changes to Roslyn analyzers

---------

Signed-off-by: Richard Murillo <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Adds `.globalconfig` to enable pattern matching

Resolves #27
…trings (ECS0004) (#41)

This pull request introduces a new Roslyn analyzer and code fix to
enforce the use of interpolated strings instead of `string.Format`. This
change addresses the recommendations outlined in Effective C#: 50
Specific Ways to Improve your C#.

## Rule Details

### Rule ID: ECS0004
**Title:** Replace string.Format with interpolated string

**Category:** Style

**Description:** This rule identifies instances of `string.Format` and
suggests replacing them with C# interpolated strings for improved
readability and reduced error potential.
Related: BillWagner/EffectiveCSharpAnalyzers#7
Closes #32

The problem addressed by this rule is the use of methods that should
involve callback mechanisms without employing delegates for this
purpose. Specifically, methods are being invoked directly without the
use of delegate types such as `Predicate<T>`, `Action<T>`, or `Func<T>`,
which are designed to handle callbacks in a type-safe and consistent
manner.
)

Resolves #35 

This is prohibited by CA1061. To implement this requirement, CA1061 is
enabled as a warning
Signed-off-by: Richard Murillo <[email protected]>
…47)

Fixes #45

```csharp
public class C {
  public void M() {
    var i = 0;
    var name = "Foo" + i;  // This is lowered to string name = string.Concat("Foo", i.ToString());
  }
}
```

Two additional checks added to the analyzer:

1. Analysis of the `SyntaxKind.AddExpression` and the
`BinaryExpressionSyntax` to look for concat. This is more for defense in
depth to stop additional special cased analysis that was there
previously.
2. Update to `OperationKind.Conversion`. When an `IConversionOperation`
is analyzed, we need to consider a few things: there are safe operations
where the compiler lowers the code to an optimal condition to avoid
boxing, boxing depends on surrounding context, and last there are
specific types that are special cased.
)

This pull request introduces a new analyzer rule, ECS0008, which is
designed to enforce the use of the null-conditional operator when
invoking event handlers in C#. This practice helps prevent potential
`NullReferenceExceptions`, improving the safety and robustness of the
code.

Key Features:
- **Detection of Potential Violations**: The rule identifies patterns
where an event handler is invoked without using the null-conditional
operator (`?.Invoke`), such as direct invocations or invocations within
an `if` statement checking for `null`.
- **Automatic Code Fixes**: The code fix provider automatically replaces
the identified pattern with the null-conditional operator, ensuring that
the event handler is only invoked when it has subscribers.
- **Comprehensive Coverage**: The rule handles cases where the event
handler is checked directly and cases where it is first assigned to a
local variable.

Example Violation:

```csharp
public class EventSource
{
    private EventHandler<int> Updated;
    private int counter;

    public void RaiseUpdates()
    {
        counter++;
        if (Updated != null)
            Updated(this, counter);
    }
}
```

Fixed code:

```csharp
public class EventSource
{
    private EventHandler<int> Updated;
    private int counter;

    public void RaiseUpdates()
    {
        counter++;
        Updated?.Invoke(this, counter);
    }
}
```
The ECS1000 analyzer for `Span<T>` is not part of Effective C# and is
removed
This pull request introduces a new Roslyn analyzer and associated code
fix provider to enforce best practices for culture-specific string
formatting in C#. The analyzer identifies cases where
`FormattableString` or `string.Create` should be used instead of string
for interpolated strings, depending on the target framework version. The
code fix provider automatically suggests the appropriate replacement.

## Changes

- Analyzer: `FormattableStringForCultureSpecificStringsAnalyzer`
- Detects culture-specific string usage and recommends either
`FormattableString`, `string.Create`, or `string.Format` based on the
.NET version.
- Code Fix Provider:
`FormattableStringForCultureSpecificStringsCodeFixProvider`
  - Provides automated fixes based on the analyzer's recommendations.
- Tests: Comprehensive unit tests covering various scenarios, including
different .NET versions and complex string interpolation cases.

This analyzer aligns modern C# and .NET practices, particularly Stephen
Toub's guidance on using `string.Create` in .NET 6+
Signed-off-by: Richard Murillo <[email protected]>
Copy link

codacy-production bot commented Aug 14, 2024

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+0.00% (target: -1.00%)
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (e6b18d1) 811 735 90.63%
Head commit (fa43e24) 811 (+0) 735 (+0) 90.63% (+0.00%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#51) 0 0 ∅ (not applicable)

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more

@rjmurillo rjmurillo added documentation Improvements or additions to documentation analyzers build dependencies Pull requests that update a dependency file releasable github_actions labels Aug 14, 2024
version.json Outdated
@@ -1,13 +1,14 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
"version": "0.2.0-alpha",
"version": "0.1.0-beta",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to merge back 0.1.0-beta, we want to ship a stable 0.1.0

## Changes

- Update build matrix to use latest version of Ubuntu and Windows
- Update release workflow to trigger when releases are published,
edited, pre-released, or released
- Move performance baseline forward
- Update .NET SDK version to 8.0.400
- Move analyzer rules to shipped
- Update numbering of analyzer rules to leave room for sub rules
- Updated analyzer strings for title, message format, description to
`LocalizableString`
- Update `version.json` to use `0.1.0`
release from the specified branches and tags, with flexibility for multiple versions within each phase.

Signed-off-by: Richard Murillo <[email protected]>
@rjmurillo rjmurillo closed this Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzers build dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation github_actions releasable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant