Skip to content

Commit 902e483

Browse files
committed
Add test for modification
1 parent ef0446d commit 902e483

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

tests/ui/unnecessary_iter_cloned.fixed

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ fn main() {
2121
let _ = check_files_ref_mut(&[(FileType::Account, path)]);
2222
let _ = check_files_self_and_arg(&[(FileType::Account, path)]);
2323
let _ = check_files_mut_path_buf(&[(FileType::Account, std::path::PathBuf::new())]);
24+
25+
check_mut_iteratee_and_modify_inner_variable();
2426
}
2527

2628
// `check_files` and its variants are based on:
@@ -138,3 +140,30 @@ fn check_files_mut_path_buf(files: &[(FileType, std::path::PathBuf)]) -> bool {
138140
fn get_file_path(_file_type: &FileType) -> Result<std::path::PathBuf, std::io::Error> {
139141
Ok(std::path::PathBuf::new())
140142
}
143+
144+
// Issue 12098
145+
// https://github.com/rust-lang/rust-clippy/issues/12098
146+
// no message emits
147+
fn check_mut_iteratee_and_modify_inner_variable() {
148+
struct Test {
149+
list: Vec<String>,
150+
mut_this: bool,
151+
}
152+
153+
impl Test {
154+
fn list(&self) -> &[String] {
155+
&self.list
156+
}
157+
}
158+
159+
let mut test = Test {
160+
list: vec![String::from("foo"), String::from("bar")],
161+
mut_this: false,
162+
};
163+
164+
for _item in test.list().to_vec() {
165+
println!("{}", _item);
166+
167+
test.mut_this = true;
168+
}
169+
}

tests/ui/unnecessary_iter_cloned.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ fn main() {
2121
let _ = check_files_ref_mut(&[(FileType::Account, path)]);
2222
let _ = check_files_self_and_arg(&[(FileType::Account, path)]);
2323
let _ = check_files_mut_path_buf(&[(FileType::Account, std::path::PathBuf::new())]);
24+
25+
check_mut_iteratee_and_modify_inner_variable();
2426
}
2527

2628
// `check_files` and its variants are based on:
@@ -138,3 +140,30 @@ fn check_files_mut_path_buf(files: &[(FileType, std::path::PathBuf)]) -> bool {
138140
fn get_file_path(_file_type: &FileType) -> Result<std::path::PathBuf, std::io::Error> {
139141
Ok(std::path::PathBuf::new())
140142
}
143+
144+
// Issue 12098
145+
// https://github.com/rust-lang/rust-clippy/issues/12098
146+
// no message emits
147+
fn check_mut_iteratee_and_modify_inner_variable() {
148+
struct Test {
149+
list: Vec<String>,
150+
mut_this: bool,
151+
}
152+
153+
impl Test {
154+
fn list(&self) -> &[String] {
155+
&self.list
156+
}
157+
}
158+
159+
let mut test = Test {
160+
list: vec![String::from("foo"), String::from("bar")],
161+
mut_this: false,
162+
};
163+
164+
for _item in test.list().to_vec() {
165+
println!("{}", _item);
166+
167+
test.mut_this = true;
168+
}
169+
}

tests/ui/unnecessary_iter_cloned.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unnecessary use of `copied`
2-
--> tests/ui/unnecessary_iter_cloned.rs:29:22
2+
--> tests/ui/unnecessary_iter_cloned.rs:31:22
33
|
44
LL | for (t, path) in files.iter().copied() {
55
| ^^^^^^^^^^^^^^^^^^^^^
@@ -17,7 +17,7 @@ LL + let other = match get_file_path(t) {
1717
|
1818

1919
error: unnecessary use of `copied`
20-
--> tests/ui/unnecessary_iter_cloned.rs:44:22
20+
--> tests/ui/unnecessary_iter_cloned.rs:46:22
2121
|
2222
LL | for (t, path) in files.iter().copied() {
2323
| ^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)