Skip to content

Commit 3ccb17c

Browse files
committed
Use with_context with format!
1 parent 5ca1add commit 3ccb17c

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

crates/rustfix/tests/parse_and_replace.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
164164

165165
debug!("next up: {:?}", file);
166166
let code = fs::read_to_string(file)?;
167-
let errors =
168-
compile_and_get_json_errors(file).context(format!("could compile {}", file.display()))?;
167+
let errors = compile_and_get_json_errors(file)
168+
.with_context(|| format!("could not compile {}", file.display()))?;
169169
let suggestions =
170170
rustfix::get_suggestions_from_json(&errors, &HashSet::new(), filter_suggestions)
171171
.context("could not load suggestions")?;
@@ -175,10 +175,8 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
175175
}
176176

177177
if std::env::var(settings::CHECK_JSON).is_ok() {
178-
let expected_json = fs::read_to_string(&json_file).context(format!(
179-
"could not load json fixtures for {}",
180-
file.display()
181-
))?;
178+
let expected_json = fs::read_to_string(&json_file)
179+
.with_context(|| format!("could not load json fixtures for {}", file.display()))?;
182180
let expected_suggestions =
183181
rustfix::get_suggestions_from_json(&expected_json, &HashSet::new(), filter_suggestions)
184182
.context("could not load expected suggestions")?;
@@ -194,7 +192,7 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
194192
}
195193

196194
let fixed = apply_suggestions(&code, &suggestions)
197-
.context(format!("could not apply suggestions to {}", file.display()))?
195+
.with_context(|| format!("could not apply suggestions to {}", file.display()))?
198196
.replace('\r', "");
199197

200198
if std::env::var(settings::RECORD_FIXED_RUST).is_ok() {
@@ -209,7 +207,7 @@ fn test_rustfix_with_file<P: AsRef<Path>>(file: P, mode: &str) -> Result<(), Err
209207
}
210208

211209
let expected_fixed = fs::read_to_string(&fixed_file)
212-
.context(format!("could read fixed file for {}", file.display()))?
210+
.with_context(|| format!("could read fixed file for {}", file.display()))?
213211
.replace('\r', "");
214212
ensure!(
215213
fixed.trim() == expected_fixed.trim(),
@@ -236,7 +234,7 @@ fn get_fixture_files(p: &str) -> Result<Vec<PathBuf>, Error> {
236234

237235
fn assert_fixtures(dir: &str, mode: &str) {
238236
let files = get_fixture_files(dir)
239-
.context(format!("couldn't load dir `{}`", dir))
237+
.with_context(|| format!("couldn't load dir `{}`", dir))
240238
.unwrap();
241239
let mut failures = 0;
242240

src/cargo/ops/cargo_add/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,10 +1085,9 @@ fn find_workspace_dep(toml_key: &str, root_manifest: &Path) -> CargoResult<Depen
10851085
.context("could not find `dependencies` table in `workspace`")?
10861086
.as_table_like()
10871087
.context("could not make `dependencies` into a table")?;
1088-
let dep_item = dependencies.get(toml_key).context(format!(
1089-
"could not find {} in `workspace.dependencies`",
1090-
toml_key
1091-
))?;
1088+
let dep_item = dependencies
1089+
.get(toml_key)
1090+
.with_context(|| format!("could not find {} in `workspace.dependencies`", toml_key))?;
10921091
Dependency::from_toml(root_manifest.parent().unwrap(), toml_key, dep_item)
10931092
}
10941093

0 commit comments

Comments
 (0)