Skip to content

Commit e686aea

Browse files
committed
Enable clippy::if_not_else
Prefer the positive condition first: `if x { .. } else { .. }` rather than `if !x { .. } else { .. }`, so the reader does not have to negate mentally to follow which branch runs. Mostly `cargo clippy --fix`. Comments that described the negated branch were moved to the branch they actually describe, and thirteen sites in datafusion-sql were done by hand because one suggestion in that crate did not type-check, which made rustfix roll back the whole crate.
1 parent 798c874 commit e686aea

96 files changed

Lines changed: 562 additions & 568 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ explicit_into_iter_loop = "allow" # 55 hits
282282
explicit_iter_loop = "allow" # 189 hits
283283
float_cmp = "allow" # 8 hits; exact float comparisons are often intentional here
284284
from_iter_instead_of_collect = "allow" # 51 hits
285-
if_not_else = "allow" # 133 hits
286285
implicit_clone = "allow" # 198 hits
287286
implicit_hasher = "allow" # 17 hits; some sites feed arrow APIs that require the default hasher
288287
inline_always = "allow" # 45 hits

‎benchmarks/src/sql_benchmark.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,15 +374,15 @@ impl SqlBenchmark {
374374

375375
// Get the first result query (assuming only one for now)
376376
let query = &self.result_queries[0];
377-
let formatted_actual_results = if !query.query.trim().is_empty() {
378-
let results = ctx.sql(&query.query).await?.collect().await?;
379-
format_record_batches(&results)
380-
} else {
377+
let formatted_actual_results = if query.query.trim().is_empty() {
381378
let actual_results = self
382379
.last_results
383380
.as_ref()
384381
.expect("last_results should be present after successful run");
385382
format_record_batches(actual_results)
383+
} else {
384+
let results = ctx.sql(&query.query).await?.collect().await?;
385+
format_record_batches(&results)
386386
}?;
387387

388388
Self::compare_results(query, &formatted_actual_results, &query.expected_result)

‎datafusion-cli/src/helper.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ pub struct CliHelper {
4949

5050
impl CliHelper {
5151
pub fn new(dialect: &Dialect, color: bool) -> Self {
52-
let highlighter: Box<dyn Highlighter> = if !color {
53-
Box::new(NoSyntaxHighlighter {})
54-
} else {
52+
let highlighter: Box<dyn Highlighter> = if color {
5553
Box::new(SyntaxHighlighter::new(dialect))
54+
} else {
55+
Box::new(NoSyntaxHighlighter {})
5656
};
5757
Self {
5858
completer: FilenameCompleter::new(),

‎datafusion-cli/src/main.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,10 @@ fn parse_batch_size(size: &str) -> Result<usize, String> {
407407
}
408408

409409
fn parse_command(command: &str) -> Result<String, String> {
410-
if !command.is_empty() {
411-
Ok(command.to_string())
412-
} else {
410+
if command.is_empty() {
413411
Err("-c flag expects only non empty commands".to_string())
412+
} else {
413+
Ok(command.to_string())
414414
}
415415
}
416416

‎datafusion-examples/examples/custom_data_source/default_column_values.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,11 @@ impl PhysicalExprAdapter for DefaultValuePhysicalExprAdapter {
317317
}
318318

319319
// Replace columns with their default literals if any
320-
let rewritten = if !replacements.is_empty() {
320+
let rewritten = if replacements.is_empty() {
321+
expr
322+
} else {
321323
let refs: HashMap<_, _> = replacements.iter().map(|(k, v)| (*k, v)).collect();
322324
replace_columns_with_literals(expr, &refs)?
323-
} else {
324-
expr
325325
};
326326

327327
// Apply the default adapter as a fallback for other schema adaptations

‎datafusion/catalog-listing/src/helpers.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,10 @@ pub async fn pruned_partition_list<'a>(
392392
file_extension: &'a str,
393393
partition_cols: &'a [(String, DataType)],
394394
) -> Result<BoxStream<'a, Result<PartitionedFile>>> {
395-
let prefix = if !partition_cols.is_empty() {
396-
evaluate_partition_prefix(partition_cols, filters)
397-
} else {
395+
let prefix = if partition_cols.is_empty() {
398396
None
397+
} else {
398+
evaluate_partition_prefix(partition_cols, filters)
399399
};
400400

401401
let objects = table_path

‎datafusion/catalog/src/streaming.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ impl StreamingTable {
158158
_filters: &[Expr],
159159
limit: Option<usize>,
160160
) -> Result<Arc<dyn ExecutionPlan>> {
161-
let physical_sort = if !self.sort_order.is_empty() {
161+
let physical_sort = if self.sort_order.is_empty() {
162+
vec![]
163+
} else {
162164
let df_schema = DFSchema::try_from(Arc::clone(&self.schema))?;
163165
let eqp = state.execution_props();
164166

@@ -182,8 +184,6 @@ impl StreamingTable {
182184
} else {
183185
original_sort_exprs
184186
}
185-
} else {
186-
vec![]
187187
};
188188

189189
let exec = StreamingTableExec::try_new(

‎datafusion/common/src/config.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4469,12 +4469,12 @@ mod tests {
44694469
#[cfg(feature = "parquet_encryption")]
44704470
impl parquet::encryption::decrypt::KeyRetriever for ParquetEncryptionKeyRetriever {
44714471
fn retrieve_key(&self, key_metadata: &[u8]) -> parquet::errors::Result<Vec<u8>> {
4472-
if !key_metadata.is_empty() {
4473-
Ok(b"1234567890123450".to_vec())
4474-
} else {
4472+
if key_metadata.is_empty() {
44754473
Err(parquet::errors::ParquetError::General(
44764474
"Key metadata not provided".to_string(),
44774475
))
4476+
} else {
4477+
Ok(b"1234567890123450".to_vec())
44784478
}
44794479
}
44804480
}

‎datafusion/common/src/datatype.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ impl FieldExt for Field {
181181
fn renamed(self, new_name: &str) -> Self {
182182
// check if this is a new name before allocating a new Field / copying
183183
// the existing one
184-
if self.name() != new_name {
185-
self.with_name(new_name)
186-
} else {
184+
if self.name() == new_name {
187185
self
186+
} else {
187+
self.with_name(new_name)
188188
}
189189
}
190190

@@ -214,10 +214,10 @@ impl FieldExt for Field {
214214
}
215215

216216
fn into_list_item(self) -> Self {
217-
if self.name() != Field::LIST_FIELD_DEFAULT_NAME {
218-
self.with_name(Field::LIST_FIELD_DEFAULT_NAME)
219-
} else {
217+
if self.name() == Field::LIST_FIELD_DEFAULT_NAME {
220218
self
219+
} else {
220+
self.with_name(Field::LIST_FIELD_DEFAULT_NAME)
221221
}
222222
}
223223
}

‎datafusion/common/src/dfschema.rs‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -631,14 +631,7 @@ impl DFSchema {
631631
/// encoded UTF8 array to be equivalent to a plain UTF8 array.
632632
pub fn has_equivalent_names_and_types(&self, other: &Self) -> Result<()> {
633633
// case 1 : schema length mismatch
634-
if self.fields().len() != other.fields().len() {
635-
_plan_err!(
636-
"Schema mismatch: the schema length are not same \
637-
Expected schema length: {}, got: {}",
638-
self.fields().len(),
639-
other.fields().len()
640-
)
641-
} else {
634+
if self.fields().len() == other.fields().len() {
642635
// case 2 : schema length match, but fields mismatch
643636
// check if the fields name are the same and have the same data types
644637
self.fields()
@@ -663,6 +656,13 @@ impl DFSchema {
663656
Ok(())
664657
}
665658
})
659+
} else {
660+
_plan_err!(
661+
"Schema mismatch: the schema length are not same \
662+
Expected schema length: {}, got: {}",
663+
self.fields().len(),
664+
other.fields().len()
665+
)
666666
}
667667
}
668668

@@ -1319,14 +1319,7 @@ impl SchemaExt for Schema {
13191319
// It is only used by insert into cases.
13201320
fn logically_equivalent_names_and_types(&self, other: &Self) -> Result<()> {
13211321
// case 1 : schema length mismatch
1322-
if self.fields().len() != other.fields().len() {
1323-
_plan_err!(
1324-
"Inserting query must have the same schema length as the table. \
1325-
Expected table schema length: {}, got: {}",
1326-
self.fields().len(),
1327-
other.fields().len()
1328-
)
1329-
} else {
1322+
if self.fields().len() == other.fields().len() {
13301323
// case 2 : schema length match, but fields mismatch
13311324
// check if the fields name are the same and have the same data types
13321325
self.fields()
@@ -1345,6 +1338,13 @@ impl SchemaExt for Schema {
13451338
Ok(())
13461339
}
13471340
})
1341+
} else {
1342+
_plan_err!(
1343+
"Inserting query must have the same schema length as the table. \
1344+
Expected table schema length: {}, got: {}",
1345+
self.fields().len(),
1346+
other.fields().len()
1347+
)
13481348
}
13491349
}
13501350
}

0 commit comments

Comments
 (0)