Skip to content

Commit f32984b

Browse files
authored
CI: add clippy::needless_pass_by_value rule (#18468)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> An initial attempt towards #18467 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ### Rationale for the additional lint rule `clippy::needless_pass_by_value` There is a clippy lint rule that is not turned on by the current strictness level in CI: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value Note it has the `Clippy` category `pedantic`, and its description is `lints which are rather strict or have occasional false positives` from https://doc.rust-lang.org/nightly/clippy It seems we have been suffering from the excessive copying issue for quite some time, and @alamb is on the front line now #18413. I think this extra lint rule is able to help. ### Implementation plan This PR only enables this rule in `datafusion-common` package, and apply `#[allow(clippy::needless_pass_by_value)]` for all violations. If this PR makes sense, we can open a tracking issue and roll out this check to the remaining workspace packages. At least this can help prevent new inefficient patterns and identify existing issues that we can fix gradually. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 7591919 commit f32984b

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

ci/scripts/rust_clippy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
# under the License.
1919

2020
set -ex
21-
cargo clippy --all-targets --workspace --features avro,pyarrow,integration-tests,extended_tests -- -D warnings
21+
cargo clippy --all-targets --workspace --features avro,pyarrow,integration-tests,extended_tests -- -D warnings

datafusion/common/src/hash_utils.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn hash_array_primitive<T>(
141141
/// with the new hash using `combine_hashes`
142142
#[cfg(not(feature = "force_hash_collisions"))]
143143
fn hash_array<T>(
144-
array: T,
144+
array: &T,
145145
random_state: &RandomState,
146146
hashes_buffer: &mut [u64],
147147
rehash: bool,
@@ -400,16 +400,16 @@ pub fn create_hashes<'a>(
400400
downcast_primitive_array! {
401401
array => hash_array_primitive(array, random_state, hashes_buffer, rehash),
402402
DataType::Null => hash_null(random_state, hashes_buffer, rehash),
403-
DataType::Boolean => hash_array(as_boolean_array(array)?, random_state, hashes_buffer, rehash),
404-
DataType::Utf8 => hash_array(as_string_array(array)?, random_state, hashes_buffer, rehash),
405-
DataType::Utf8View => hash_array(as_string_view_array(array)?, random_state, hashes_buffer, rehash),
406-
DataType::LargeUtf8 => hash_array(as_largestring_array(array), random_state, hashes_buffer, rehash),
407-
DataType::Binary => hash_array(as_generic_binary_array::<i32>(array)?, random_state, hashes_buffer, rehash),
408-
DataType::BinaryView => hash_array(as_binary_view_array(array)?, random_state, hashes_buffer, rehash),
409-
DataType::LargeBinary => hash_array(as_generic_binary_array::<i64>(array)?, random_state, hashes_buffer, rehash),
403+
DataType::Boolean => hash_array(&as_boolean_array(array)?, random_state, hashes_buffer, rehash),
404+
DataType::Utf8 => hash_array(&as_string_array(array)?, random_state, hashes_buffer, rehash),
405+
DataType::Utf8View => hash_array(&as_string_view_array(array)?, random_state, hashes_buffer, rehash),
406+
DataType::LargeUtf8 => hash_array(&as_largestring_array(array), random_state, hashes_buffer, rehash),
407+
DataType::Binary => hash_array(&as_generic_binary_array::<i32>(array)?, random_state, hashes_buffer, rehash),
408+
DataType::BinaryView => hash_array(&as_binary_view_array(array)?, random_state, hashes_buffer, rehash),
409+
DataType::LargeBinary => hash_array(&as_generic_binary_array::<i64>(array)?, random_state, hashes_buffer, rehash),
410410
DataType::FixedSizeBinary(_) => {
411411
let array: &FixedSizeBinaryArray = array.as_any().downcast_ref().unwrap();
412-
hash_array(array, random_state, hashes_buffer, rehash)
412+
hash_array(&array, random_state, hashes_buffer, rehash)
413413
}
414414
DataType::Dictionary(_, _) => downcast_dictionary_array! {
415415
array => hash_dictionary(array, random_state, hashes_buffer, rehash)?,

datafusion/common/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
// Make sure fast / cheap clones on Arc are explicit:
2424
// https://github.com/apache/datafusion/issues/11143
2525
#![deny(clippy::clone_on_ref_ptr)]
26+
// https://github.com/apache/datafusion/issues/18503
27+
#![deny(clippy::needless_pass_by_value)]
28+
// This lint rule is enforced in `../Cargo.toml`, but it's okay to skip them in tests
29+
// See details in https://github.com/apache/datafusion/issues/18503
30+
#![cfg_attr(test, allow(clippy::needless_pass_by_value))]
2631

2732
mod column;
2833
mod dfschema;

datafusion/common/src/scalar/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4648,9 +4648,9 @@ impl fmt::Display for ScalarValue {
46484648
}
46494649
None => write!(f, "NULL")?,
46504650
},
4651-
ScalarValue::List(arr) => fmt_list(arr.to_owned() as ArrayRef, f)?,
4652-
ScalarValue::LargeList(arr) => fmt_list(arr.to_owned() as ArrayRef, f)?,
4653-
ScalarValue::FixedSizeList(arr) => fmt_list(arr.to_owned() as ArrayRef, f)?,
4651+
ScalarValue::List(arr) => fmt_list(arr.as_ref(), f)?,
4652+
ScalarValue::LargeList(arr) => fmt_list(arr.as_ref(), f)?,
4653+
ScalarValue::FixedSizeList(arr) => fmt_list(arr.as_ref(), f)?,
46544654
ScalarValue::Date32(e) => format_option!(
46554655
f,
46564656
e.map(|v| {
@@ -4772,12 +4772,11 @@ impl fmt::Display for ScalarValue {
47724772
}
47734773
}
47744774

4775-
fn fmt_list(arr: ArrayRef, f: &mut fmt::Formatter) -> fmt::Result {
4775+
fn fmt_list(arr: &dyn Array, f: &mut fmt::Formatter) -> fmt::Result {
47764776
// ScalarValue List, LargeList, FixedSizeList should always have a single element
47774777
assert_eq!(arr.len(), 1);
47784778
let options = FormatOptions::default().with_display_error(true);
4779-
let formatter =
4780-
ArrayFormatter::try_new(arr.as_ref() as &dyn Array, &options).unwrap();
4779+
let formatter = ArrayFormatter::try_new(arr, &options).unwrap();
47814780
let value_formatter = formatter.value(0);
47824781
write!(f, "{value_formatter}")
47834782
}

datafusion/common/src/scalar/struct_builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl ScalarStructBuilder {
8383
}
8484

8585
/// Add the specified field and `ScalarValue` to the struct.
86+
#[expect(clippy::needless_pass_by_value)] // Skip for public API's compatibility
8687
pub fn with_scalar(self, field: impl IntoFieldRef, value: ScalarValue) -> Self {
8788
// valid scalar value should not fail
8889
let array = value.to_array().unwrap();

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ impl LogicalPlan {
11561156

11571157
/// Helper for [Self::with_new_exprs] to use when no expressions are expected.
11581158
#[inline]
1159-
#[allow(clippy::needless_pass_by_value)] // expr is moved intentionally to ensure it's not used again
1159+
#[expect(clippy::needless_pass_by_value)] // expr is moved intentionally to ensure it's not used again
11601160
fn assert_no_expressions(&self, expr: Vec<Expr>) -> Result<()> {
11611161
if !expr.is_empty() {
11621162
return internal_err!("{self:?} should have no exprs, got {:?}", expr);
@@ -1166,7 +1166,7 @@ impl LogicalPlan {
11661166

11671167
/// Helper for [Self::with_new_exprs] to use when no inputs are expected.
11681168
#[inline]
1169-
#[allow(clippy::needless_pass_by_value)] // inputs is moved intentionally to ensure it's not used again
1169+
#[expect(clippy::needless_pass_by_value)] // inputs is moved intentionally to ensure it's not used again
11701170
fn assert_no_inputs(&self, inputs: Vec<LogicalPlan>) -> Result<()> {
11711171
if !inputs.is_empty() {
11721172
return internal_err!("{self:?} should have no inputs, got: {:?}", inputs);

0 commit comments

Comments
 (0)