Skip to content

Commit e22e75c

Browse files
Semi-automated code cleanup (pgcentralfoundation#2189)
Automated `--fix`es combined with the occasional hand-retouching, to smooth things out after having churned large parts of cargo-pgrx.
1 parent fef7117 commit e22e75c

11 files changed

Lines changed: 108 additions & 133 deletions

File tree

cargo-pgrx/src/command/install.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,16 @@ fn copy_sql_files(
355355
skip_build: bool,
356356
output_tracking: &mut Vec<PathBuf>,
357357
) -> eyre::Result<()> {
358-
let (_, extname) = find_control_file(&package_manifest_path)?;
358+
let (_, extname) = find_control_file(package_manifest_path)?;
359359
{
360-
let version = get_version(&package_manifest_path)?;
360+
let version = get_version(package_manifest_path)?;
361361
let filename = format!("{extname}--{version}.sql");
362362
let dest = extdir.join(filename);
363363

364364
crate::command::schema::generate_schema(
365365
user_manifest_path,
366366
user_package,
367-
package_manifest_path.as_ref(),
367+
package_manifest_path,
368368
profile,
369369
is_test,
370370
features,
@@ -392,7 +392,7 @@ fn copy_sql_files(
392392
extdir.join(filename),
393393
"extension schema upgrade file",
394394
true,
395-
&package_manifest_path,
395+
package_manifest_path,
396396
output_tracking,
397397
)?;
398398
}
@@ -453,10 +453,10 @@ pub(crate) fn get_version(manifest_path: &Path) -> eyre::Result<String> {
453453
return Ok(version.clone());
454454
}
455455

456-
let version = match get_property(&manifest_path, "default_version")? {
456+
let version = match get_property(manifest_path, "default_version")? {
457457
Some(v) => {
458458
if v == "@CARGO_VERSION@" {
459-
let metadata = crate::metadata::metadata(&Default::default(), Some(&manifest_path))
459+
let metadata = crate::metadata::metadata(&Default::default(), Some(manifest_path))
460460
.wrap_err("couldn't get cargo metadata")?;
461461
crate::metadata::validate(Some(manifest_path), &metadata)?;
462462
let manifest_path = crate::manifest::manifest_path(&metadata, None)
@@ -554,10 +554,10 @@ fn filter_contents(manifest_path: &Path, mut input: String) -> eyre::Result<Stri
554554
// avoid doing this if we don't actually have the token
555555
// the project might not be a git repo so running `git`
556556
// would fail
557-
input = input.replace("@GIT_HASH@", &get_git_hash(&manifest_path)?);
557+
input = input.replace("@GIT_HASH@", &get_git_hash(manifest_path)?);
558558
}
559559

560-
input = input.replace("@CARGO_VERSION@", &get_version(&manifest_path)?);
560+
input = input.replace("@CARGO_VERSION@", &get_version(manifest_path)?);
561561

562562
Ok(input)
563563
}

cargo-pgrx/src/command/regress.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ impl Regress {
8282
return true;
8383
}
8484

85-
let sql = manifest_path_to_sql_tests_path(&manifest_path);
85+
let sql = manifest_path_to_sql_tests_path(manifest_path);
8686
if !sql.exists() { return false; }
87-
let expected = manifest_path_to_expected_tests_output_path(&manifest_path);
87+
let expected = manifest_path_to_expected_tests_output_path(manifest_path);
8888
if !expected.exists() {return false; }
8989

9090
let setup_sql = sql.join("setup.sql");
@@ -164,12 +164,10 @@ impl Regress {
164164

165165
// `setup.{only}` is a special file that we handle separately
166166
let is_setup = |entry: &DirEntry| {
167-
if let Some(filename) = entry.file_name().to_str() {
168-
if filename.ends_with(&format!("setup.{only}")) {
169-
return true;
170-
}
171-
}
172-
false
167+
entry
168+
.file_name()
169+
.to_str()
170+
.is_some_and(|filename| filename.ends_with(&format!("setup.{only}")))
173171
};
174172

175173
// remove the "setup" file from the list
@@ -179,10 +177,10 @@ impl Regress {
179177
files.sort_unstable_by_key(|entry| entry.file_name());
180178

181179
// if we detected a setup file and the caller wants to include it, make it the first entry
182-
if let Some(setup_entry) = setup_entry {
183-
if include_setup {
184-
files.insert(0, setup_entry);
185-
}
180+
if let Some(setup_entry) = setup_entry
181+
&& include_setup
182+
{
183+
files.insert(0, setup_entry);
186184
}
187185
}
188186

@@ -201,7 +199,7 @@ impl Regress {
201199
.to_str()
202200
.expect("test result output filename should be valid UTF8")
203201
.to_string();
204-
let test_output = std::fs::read_to_string(&test_result_output)?;
202+
let test_output = std::fs::read_to_string(test_result_output)?;
205203

206204
let variant_suffix: Option<String>;
207205

@@ -289,12 +287,12 @@ impl Regress {
289287
for new_test in new_tests {
290288
if let Some(test_result_output) = create_regress_output(
291289
pg_config,
292-
&manifest_path,
293-
&pgregress_path,
290+
manifest_path,
291+
pgregress_path,
294292
dbname,
295293
new_test,
296294
)? {
297-
self.accept_new_test(&manifest_path, &test_result_output, auto)?;
295+
self.accept_new_test(manifest_path, &test_result_output, auto)?;
298296
}
299297
}
300298
}
@@ -304,14 +302,14 @@ impl Regress {
304302

305303
if !success && auto {
306304
// tests failed, but the user asked to `auto`matically accept their output as new output
307-
let results_files = self.list_results_outputs(&manifest_path, include_setup)?;
305+
let results_files = self.list_results_outputs(manifest_path, include_setup)?;
308306

309307
println!();
310308
for entry in results_files {
311309
let filename =
312310
entry.file_name().to_str().expect("filename should be valid UTF8").to_owned();
313311
let expected_path =
314-
manifest_path_to_expected_tests_output_path(&manifest_path).join(filename);
312+
manifest_path_to_expected_tests_output_path(manifest_path).join(filename);
315313

316314
if !expected_path.exists() {
317315
// this is a file from `results/test-name.out` for which we don't have an expected file
@@ -455,7 +453,7 @@ fn create_regress_output(
455453
// doesn't exist, since we are creating the test output. So if that's the case, if we have
456454
// a `.out` file for it in the results/ directory, then we're successful
457455
let out_file =
458-
manifest_path_to_results_output_path(&manifest_path).join(format!("{test_name}.out"));
456+
manifest_path_to_results_output_path(manifest_path).join(format!("{test_name}.out"));
459457
if out_file.exists() {
460458
return Ok(Some(out_file));
461459
} else {
@@ -668,10 +666,11 @@ fn manifest_path_to_results_output_path(manifest_path: &Path) -> PathBuf {
668666
}
669667

670668
fn add_to_git(path: &Path) -> eyre::Result<()> {
671-
if let Ok(git) = which::which("git") {
672-
if is_git_repo(&git) && !Command::new(git).arg("add").arg(path).status()?.success() {
673-
panic!("unable to add {} to git", path.display());
674-
}
669+
if let Ok(git) = which::which("git")
670+
&& is_git_repo(&git)
671+
&& !Command::new(git).arg("add").arg(path).status()?.success()
672+
{
673+
panic!("unable to add {} to git", path.display());
675674
}
676675
Ok(())
677676
}

cargo-pgrx/src/command/schema.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub(crate) fn generate_schema_for_cli(
135135
skip_build: bool,
136136
output_tracking: &mut Vec<PathBuf>,
137137
) -> eyre::Result<()> {
138-
let manifest = Manifest::from_path(&package_manifest_path)?;
138+
let manifest = Manifest::from_path(package_manifest_path)?;
139139
let features_arg = features.features.join(" ");
140140

141141
let package_name = if let Some(user_package) = user_package {
@@ -180,7 +180,7 @@ pub(crate) fn generate_schema_implicit(
180180
output_tracking: &mut Vec<PathBuf>,
181181
manifest: cargo_toml::Manifest,
182182
) -> eyre::Result<()> {
183-
let (control_file, _extname) = find_control_file(&package_manifest_path)?;
183+
let (control_file, _extname) = find_control_file(package_manifest_path)?;
184184
let lib_name = manifest.lib_name()?;
185185
let lib_filename = manifest.lib_filename()?;
186186

@@ -396,7 +396,7 @@ fn compute_codegen(
396396
out
397397
};
398398
let build = {
399-
let versioned_so = get_property(&package_manifest_path, "module_pathname")?.is_none();
399+
let versioned_so = get_property(package_manifest_path, "module_pathname")?.is_none();
400400
quote::quote! {
401401
let pgrx_sql = ::pgrx::pgrx_sql_entity_graph::PgrxSql::build(
402402
entities.into_iter(),
@@ -525,15 +525,15 @@ fn pgrx_embed_name(manifest: &Manifest) -> eyre::Result<String> {
525525

526526
let package_name = name_from(&manifest.package_name()?);
527527
let lib_name = name_from(&manifest.lib_name()?);
528-
(&manifest.bin)
529-
.into_iter()
528+
manifest
529+
.bin
530+
.iter()
530531
.find(|bin| {
531532
// As cargo_anifest autofills lib.name if it's empty, it's impossible to
532533
// check only against one name. Perhaps, cargo-util-schemas can help with that.
533534
bin.name.as_ref().is_some_and(|name| name == &package_name || name == &lib_name)
534535
})
535-
.map(|bin| bin.name.to_owned())
536-
.flatten()
536+
.and_then(|bin| bin.name.to_owned())
537537
.ok_or_else(|| eyre!("Failed to find a pgrx_embed binary."))
538538
}
539539

cargo-pgrx/src/command/start.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ pub(crate) fn start_postgres(
146146
command.arg("--time-stamp=yes");
147147
command.arg("--error-markers=VALGRINDERROR-BEGIN,VALGRINDERROR-END");
148148
command.arg("--trace-children=yes");
149-
if let Ok(path) = valgrind_suppressions_path(&pg_config) {
150-
if let Ok(true) = std::fs::exists(&path) {
151-
command.arg(format!("--suppressions={}", path.display()));
152-
}
149+
if let Ok(path) = valgrind_suppressions_path(pg_config)
150+
&& let Ok(true) = std::fs::exists(&path)
151+
{
152+
command.arg(format!("--suppressions={}", path.display()));
153153
}
154154
command.arg(pg_config.postmaster_path()?.display().to_string());
155155
file.write_all(format!("{command:?}").as_bytes())?;

cargo-pgrx/src/command/version.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ mod rss {
7979
let major = major.unwrap().parse::<u16>().unwrap_or_default();
8080
let minor = minor.unwrap().parse::<u16>().unwrap_or_default();
8181

82-
if let Some(known_pgver) = versions.get_mut(&major) {
83-
if matches!(known_pgver.minor, PgMinorVersion::Latest) {
84-
// fill in the latest minor version number and its url
85-
known_pgver.minor = PgMinorVersion::Release(minor);
86-
known_pgver.url = Some(Url::parse(&download_url(major, minor))?);
87-
}
82+
if let Some(known_pgver) = versions.get_mut(&major)
83+
&& matches!(known_pgver.minor, PgMinorVersion::Latest)
84+
{
85+
// fill in the latest minor version number and its url
86+
known_pgver.minor = PgMinorVersion::Release(minor);
87+
known_pgver.url = Some(Url::parse(&download_url(major, minor))?);
8888
}
8989
}
9090

cargo-pgrx/src/manifest.rs

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,21 @@ pub(crate) fn modify_features_for_version(
7575
test: bool,
7676
) {
7777
if let Some(features) = features {
78-
if let Some(default_features) = manifest.features.get("default") {
79-
if !features.no_default_features {
80-
// if the user didn't specify `--no-default-features`, which would otherwise indicate
81-
// they think they know what they're doing, we need to build an explicit set of features
82-
// to use and turn on `--no-default-features`
83-
84-
features.no_default_features = true;
85-
features.features.extend(
86-
default_features
87-
.iter()
88-
// only include default features that aren't known pgXX version features
89-
.filter(|flag| !pgrx.is_feature_flag(flag))
90-
.cloned(),
91-
);
92-
}
78+
if let Some(default_features) = manifest.features.get("default")
79+
&& !features.no_default_features
80+
{
81+
// if the user didn't specify `--no-default-features`, which would otherwise indicate
82+
// they think they know what they're doing, we need to build an explicit set of features
83+
// to use and turn on `--no-default-features`
84+
85+
features.no_default_features = true;
86+
features.features.extend(
87+
default_features
88+
.iter()
89+
// only include default features that aren't known pgXX version features
90+
.filter(|flag| !pgrx.is_feature_flag(flag))
91+
.cloned(),
92+
);
9393
}
9494

9595
// if we know we're running from the `pgrx-tests/src/framework.rs`, remove any user-specified features
@@ -132,35 +132,25 @@ pub(crate) fn pg_config_and_version(
132132
} else if let Some(features) = user_features.as_ref() {
133133
// the user did not give us an explicit Postgres version, so see if there's one in the set
134134
// of `--feature` flags they gave us
135-
for flag in &features.features {
136-
if pgrx.is_feature_flag(flag) {
137-
// use the first feature flag that is a Postgres version we support
138-
return Some(PgVersionSource::FeatureFlag(flag.clone()));
139-
}
135+
if let Some(flag) = features.features.iter().find(|flag| pgrx.is_feature_flag(flag)) {
136+
// use the first feature flag that is a Postgres version we support
137+
return Some(PgVersionSource::FeatureFlag(flag.clone()));
140138
}
141139

142140
// user didn't give us a feature flag that is a Postgres version
143141

144142
// if they didn't ask for `--no-default-features` lets see if we have a default
145143
// postgres version feature specified in the manifest
146-
if !features.no_default_features {
147-
if let Some(default_features) = manifest.features.get("default") {
148-
for flag in default_features {
149-
if pgrx.is_feature_flag(flag) {
150-
return Some(PgVersionSource::DefaultFeature(flag.clone()));
151-
}
152-
}
153-
}
154-
}
155-
} else {
156-
// lets check the manifest for a default feature
157-
if let Some(default_features) = manifest.features.get("default") {
158-
for flag in default_features {
159-
if pgrx.is_feature_flag(flag) {
160-
return Some(PgVersionSource::DefaultFeature(flag.clone()));
161-
}
162-
}
144+
if !features.no_default_features
145+
&& let Some(default_features) = manifest.features.get("default")
146+
&& let Some(flag) = default_features.iter().find(|flag| pgrx.is_feature_flag(flag))
147+
{
148+
return Some(PgVersionSource::DefaultFeature(flag.clone()));
163149
}
150+
} else if let Some(default_features) = manifest.features.get("default")
151+
&& let Some(flag) = default_features.iter().find(|flag| pgrx.is_feature_flag(flag))
152+
{
153+
return Some(PgVersionSource::DefaultFeature(flag.clone()));
164154
}
165155

166156
// we cannot determine the Postgres version the user wants to use

pgrx-bindgen/src/build/clang.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ pub(crate) fn detect_include_paths_for(
4040
// so we can only use clangs that match bindgen's libclang major version.
4141
if let Some(ClangSys { path, version: Some(v), c_search_paths, .. }) =
4242
ClangSys::find(preferred_clang, &[])
43+
&& Some(&*path) == preferred_clang
44+
&& v.Major as u32 == clang_major
4345
{
44-
if Some(&*path) == preferred_clang && v.Major as u32 == clang_major {
45-
return (false, c_search_paths.unwrap_or_default());
46-
}
46+
return (false, c_search_paths.unwrap_or_default());
4747
}
4848

4949
// Oh no, still here?

0 commit comments

Comments
 (0)