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

Introduce StringIs{,Not}EmptyPredicate Refaster rules #577

Merged

Conversation

mohamedsamehsalah
Copy link
Contributor

Original suggestion here.

Suggested commit message:

Prefer `not(String::isEmpty) over `s -> !s.isEmpty()` for Predicates

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/prefer-string-filter-Predicate-not-isEmpty branch from 5b9e3e1 to b6d3c6a Compare April 10, 2023 15:50
@@ -39,6 +39,8 @@ ImmutableSet<Boolean> testStringIsNullOrEmpty() {
getClass().getName() != null && !getClass().getName().isEmpty());
}

// XXX: Consider updating qualifier `Predicate#not` if/when conditional `ImportPolicy` is
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Allow more control over which methods are statically imported by @UseImportPolicy. Sometimes the @AfterTemplate contains more than one static method invocation, and only a subset should be statically imported.

See here

Copy link
Member

Choose a reason for hiding this comment

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

Check. Effectively we won't do this, I think. Instead we'll just rely on the existing StaticImport check.

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Optional.ofNullable(toString()).filter(s -> !s.isEmpty()),
Optional.ofNullable(toString()).filter(s -> !s.isEmpty()));
Optional.ofNullable(toString()).filter(Predicate.not(String::isEmpty)),
Optional.ofNullable(toString()).filter(Predicate.not(String::isEmpty)),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be a candidate for static import later.
See here

@mohamedsamehsalah mohamedsamehsalah changed the title Prefer not(String::isEmpty) over s -> !s.isEmpty()` when filtering Prefer not(String::isEmpty) over s -> !s.isEmpty()` for Predicates Apr 10, 2023
Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

Tnx for picking this up @mohamedsamehsalah! I added a commit with a proposal.

Suggested commit message:

Introduce `StringIs{,Not}EmptyPredicate` Refaster rules (#577)

}
}

static final class FilterEmptyString {
@BeforeTemplate
Optional<String> before(Optional<String> optional) {
return optional.map(Strings::emptyToNull);
return Refaster.anyOf(optional.map(Strings::emptyToNull), optional.filter(s -> !s.isEmpty()));
Copy link
Member

Choose a reason for hiding this comment

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

Now the rule is very specific to this case. Instead we can introduce a separate rule like we have for EqualsPredicate. (In general we always try to go the smallest/most generic rule.)

@@ -39,6 +39,8 @@ ImmutableSet<Boolean> testStringIsNullOrEmpty() {
getClass().getName() != null && !getClass().getName().isEmpty());
}

// XXX: Consider updating qualifier `Predicate#not` if/when conditional `ImportPolicy` is
Copy link
Member

Choose a reason for hiding this comment

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

Check. Effectively we won't do this, I think. Instead we'll just rely on the existing StaticImport check.

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202 Stephan202 added this to the 0.10.0 milestone Apr 10, 2023
Comment on lines 202 to 204
- Allow more control over _which_ methods are statically imported by
`@UseImportPolicy`. Sometimes the `@AfterTemplate` contains more than one
static method invocation, and only a subset should be statically imported.
Copy link
Member

Choose a reason for hiding this comment

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

Changed my mind about removing this, as I just noticed that this is under the heading "Refaster extension ideas". Will revert. (Actually moving this section elsewhere is for another day.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(Actually moving this section elsewhere is for another day.)

I can pick that up if you tell me where it is suposed to be 😅

Copy link
Member

Choose a reason for hiding this comment

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

Haha, not sure. Eventually we should likely have additional sections on the website, but that requires first knowing what we want. (And even then the text should likely be reviewed first, etc.)

But I'll keep an eye out for other possible PRs! 😄

Copy link
Member

Choose a reason for hiding this comment

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

Alright, maybe you'd be interested in this one: #578 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you 🚀

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

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

Nice improvements, curious to see the impact on our codebase 🚀 !

@werli can you take a look? 😄

Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

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

I meant to press approve 😅.

@rickie rickie force-pushed the mohamedsamehsalah/prefer-string-filter-Predicate-not-isEmpty branch from 4d2cb98 to c316c88 Compare April 13, 2023 07:50
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie force-pushed the mohamedsamehsalah/prefer-string-filter-Predicate-not-isEmpty branch from c316c88 to 2e52821 Compare April 15, 2023 14:59
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/prefer-string-filter-Predicate-not-isEmpty branch from 2e52821 to 2cceebb Compare April 16, 2023 20:12
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@werli werli left a comment

Choose a reason for hiding this comment

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

Let's indeed generalize this in MethodReferenceUsage. 👍

Small step for now, but should be solved on a larger scale.

@mohamedsamehsalah mohamedsamehsalah force-pushed the mohamedsamehsalah/prefer-string-filter-Predicate-not-isEmpty branch from 2cceebb to 96b5773 Compare April 18, 2023 06:24
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie changed the title Prefer not(String::isEmpty) over s -> !s.isEmpty()` for Predicates Introduce StringIs{,Not}EmptyPredicate Refaster rules Apr 18, 2023
@rickie rickie merged commit 3af81d8 into master Apr 18, 2023
@rickie rickie deleted the mohamedsamehsalah/prefer-string-filter-Predicate-not-isEmpty branch April 18, 2023 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

4 participants