-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Suggest using Path for comparing extensions #10107
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bd83650
Suggest using Path for comparing extensions
tylerjw 0aa7d73
Only remove method from end of recv, indent suggestion source
tylerjw 0cee2c5
Don't trigger lint if last method is to_lower/upper
tylerjw ea6ff7e
Apply changes suggested in review
tylerjw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// run-rustfix | ||
#![warn(clippy::case_sensitive_file_extension_comparisons)] | ||
|
||
use std::string::String; | ||
|
||
struct TestStruct; | ||
|
||
impl TestStruct { | ||
fn ends_with(self, _arg: &str) {} | ||
} | ||
|
||
#[allow(dead_code)] | ||
fn is_rust_file(filename: &str) -> bool { | ||
std::path::Path::new(filename) | ||
.extension() | ||
.map_or(false, |ext| ext.eq_ignore_ascii_case("rs")) | ||
} | ||
|
||
fn main() { | ||
// std::string::String and &str should trigger the lint failure with .ext12 | ||
let _ = std::path::Path::new(&String::new()) | ||
.extension() | ||
.map_or(false, |ext| ext.eq_ignore_ascii_case("ext12")); | ||
let _ = std::path::Path::new("str") | ||
.extension() | ||
.map_or(false, |ext| ext.eq_ignore_ascii_case("ext12")); | ||
|
||
// The fixup should preserve the indentation level | ||
{ | ||
let _ = std::path::Path::new("str") | ||
.extension() | ||
.map_or(false, |ext| ext.eq_ignore_ascii_case("ext12")); | ||
} | ||
|
||
// The test struct should not trigger the lint failure with .ext12 | ||
TestStruct {}.ends_with(".ext12"); | ||
|
||
// std::string::String and &str should trigger the lint failure with .EXT12 | ||
let _ = std::path::Path::new(&String::new()) | ||
.extension() | ||
.map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12")); | ||
let _ = std::path::Path::new("str") | ||
.extension() | ||
.map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12")); | ||
|
||
// Should not trigger the lint failure because of the calls to to_lowercase and to_uppercase | ||
let _ = String::new().to_lowercase().ends_with(".EXT12"); | ||
let _ = String::new().to_uppercase().ends_with(".EXT12"); | ||
|
||
// The test struct should not trigger the lint failure with .EXT12 | ||
TestStruct {}.ends_with(".EXT12"); | ||
|
||
// Should not trigger the lint failure with .eXT12 | ||
let _ = String::new().ends_with(".eXT12"); | ||
let _ = "str".ends_with(".eXT12"); | ||
TestStruct {}.ends_with(".eXT12"); | ||
|
||
// Should not trigger the lint failure with .EXT123 (too long) | ||
let _ = String::new().ends_with(".EXT123"); | ||
let _ = "str".ends_with(".EXT123"); | ||
TestStruct {}.ends_with(".EXT123"); | ||
|
||
// Shouldn't fail if it doesn't start with a dot | ||
let _ = String::new().ends_with("a.ext"); | ||
let _ = "str".ends_with("a.extA"); | ||
TestStruct {}.ends_with("a.ext"); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 55 additions & 11 deletions
66
tests/ui/case_sensitive_file_extension_comparisons.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,87 @@ | ||
error: case-sensitive file extension comparison | ||
--> $DIR/case_sensitive_file_extension_comparisons.rs:12:14 | ||
--> $DIR/case_sensitive_file_extension_comparisons.rs:14:5 | ||
| | ||
LL | filename.ends_with(".rs") | ||
| ^^^^^^^^^^^^^^^^ | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider using a case-insensitive comparison instead | ||
= note: `-D clippy::case-sensitive-file-extension-comparisons` implied by `-D warnings` | ||
help: use std::path::Path | ||
| | ||
LL ~ std::path::Path::new(filename) | ||
LL + .extension() | ||
LL + .map_or(false, |ext| ext.eq_ignore_ascii_case("rs")) | ||
| | ||
|
||
error: case-sensitive file extension comparison | ||
--> $DIR/case_sensitive_file_extension_comparisons.rs:17:27 | ||
--> $DIR/case_sensitive_file_extension_comparisons.rs:19:13 | ||
| | ||
LL | let _ = String::new().ends_with(".ext12"); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider using a case-insensitive comparison instead | ||
help: use std::path::Path | ||
| | ||
LL ~ let _ = std::path::Path::new(&String::new()) | ||
LL + .extension() | ||
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12")); | ||
| | ||
|
||
error: case-sensitive file extension comparison | ||
--> $DIR/case_sensitive_file_extension_comparisons.rs:18:19 | ||
--> $DIR/case_sensitive_file_extension_comparisons.rs:20:13 | ||
| | ||
LL | let _ = "str".ends_with(".ext12"); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider using a case-insensitive comparison instead | ||
help: use std::path::Path | ||
| | ||
LL ~ let _ = std::path::Path::new("str") | ||
LL + .extension() | ||
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12")); | ||
| | ||
|
||
error: case-sensitive file extension comparison | ||
--> $DIR/case_sensitive_file_extension_comparisons.rs:24:27 | ||
--> $DIR/case_sensitive_file_extension_comparisons.rs:24:17 | ||
| | ||
LL | let _ = "str".ends_with(".ext12"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider using a case-insensitive comparison instead | ||
help: use std::path::Path | ||
| | ||
LL ~ let _ = std::path::Path::new("str") | ||
LL + .extension() | ||
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("ext12")); | ||
| | ||
|
||
error: case-sensitive file extension comparison | ||
--> $DIR/case_sensitive_file_extension_comparisons.rs:31:13 | ||
| | ||
LL | let _ = String::new().ends_with(".EXT12"); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider using a case-insensitive comparison instead | ||
help: use std::path::Path | ||
| | ||
LL ~ let _ = std::path::Path::new(&String::new()) | ||
LL + .extension() | ||
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12")); | ||
| | ||
|
||
error: case-sensitive file extension comparison | ||
--> $DIR/case_sensitive_file_extension_comparisons.rs:25:19 | ||
--> $DIR/case_sensitive_file_extension_comparisons.rs:32:13 | ||
| | ||
LL | let _ = "str".ends_with(".EXT12"); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider using a case-insensitive comparison instead | ||
help: use std::path::Path | ||
| | ||
LL ~ let _ = std::path::Path::new("str") | ||
LL + .extension() | ||
LL ~ .map_or(false, |ext| ext.eq_ignore_ascii_case("EXT12")); | ||
| | ||
|
||
error: aborting due to 5 previous errors | ||
error: aborting due to 6 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.