Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: enable clippy::ref_as_ptr #5577

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ too_many_lines = "allow"
must_use_candidate = "allow"
# used_underscore_binding= "allow"
doc_markdown = "allow"
ref_as_ptr = "allow" # FIXME or give a reason
# nursery
# `const` functions do not make sense for our project because this is not a `const` library.
# This rule also confuses newcomers and forces them to add `const` blindlessly without any reason.
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_ast/src/ast/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,10 +891,10 @@ macro_rules! shared_enum_variants {
#[inline]
pub fn $as_child(&self) -> Option<&$child<'a>> {
if self.$is_child() {
#[allow(unsafe_code, clippy::ptr_as_ptr)]
#[allow(unsafe_code)]
// SAFETY: Transmute is safe because discriminants + types are identical between
// `$parent` and `$child` for $child variants
Some(unsafe { &*(self as *const _ as *const $child) })
Some(unsafe { &*std::ptr::from_ref(self).cast::<$child>() })
} else {
None
}
Expand All @@ -904,10 +904,10 @@ macro_rules! shared_enum_variants {
#[inline]
pub fn $as_child_mut(&mut self) -> Option<&mut $child<'a>> {
if self.$is_child() {
#[allow(unsafe_code, clippy::ptr_as_ptr)]
#[allow(unsafe_code)]
// SAFETY: Transmute is safe because discriminants + types are identical between
// `$parent` and `$child` for $child variants
Some(unsafe { &mut *(self as *mut _ as *mut $child) })
Some(unsafe { &mut *std::ptr::from_mut(self).cast::<$child>() })
} else {
None
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_index/src/idxslice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ impl<I: Idx, T> IndexSlice<I, [T]> {
pub fn from_slice(s: &[T]) -> &Self {
// SAFETY: `IndexSlice` is a thin wrapper around `[T]` with the added marker for the index.

unsafe { &*(s as *const [T] as *const Self) }
unsafe { &*(core::ptr::from_ref::<[T]>(s) as *const Self) }
}

/// Construct a new mutable IdxSlice by wrapping an existing mutable slice.
#[inline(always)]
pub fn from_slice_mut(s: &mut [T]) -> &mut Self {
// SAFETY: `IndexSlice` is a thin wrapper around `[T]` with the added marker for the index.

unsafe { &mut *(s as *mut [T] as *mut Self) }
unsafe { &mut *(core::ptr::from_mut::<[T]>(s) as *mut Self) }
}

/// Copies `self` into a new `IndexVec`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Rule for NoUselessSwitchCase {
let default_case = default_cases[0];

// Check if the `default` case is the last case
if default_case as *const _ != cases.last().unwrap() as *const _ {
if std::ptr::from_ref(default_case) != std::ptr::from_ref(cases.last().unwrap()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/unicorn/prefer_event_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Rule for PreferEventTarget {
return;
};

if ident as *const _ != std::ptr::addr_of!(**callee_ident) {
if std::ptr::from_ref(ident) != std::ptr::addr_of!(**callee_ident) {
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/unicorn/prefer_regexp_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Rule for PreferRegexpTest {
};

// Check if the `test` of the for statement is the same node as the call expression.
if std::ptr::addr_of!(**call_expr2) != call_expr as *const _ {
if std::ptr::addr_of!(**call_expr2) != std::ptr::from_ref(call_expr) {
return;
}
}
Expand All @@ -97,7 +97,7 @@ impl Rule for PreferRegexpTest {
};

// Check if the `test` of the conditional expression is the same node as the call expression.
if std::ptr::addr_of!(**call_expr2) != call_expr as *const _ {
if std::ptr::addr_of!(**call_expr2) != std::ptr::from_ref(call_expr) {
return;
}
}
Expand Down
22 changes: 10 additions & 12 deletions crates/oxc_traverse/scripts/lib/walk.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export default function generateWalkFunctionsCode(types) {
clippy::missing_panics_doc,
clippy::undocumented_unsafe_blocks,
clippy::semicolon_if_nothing_returned,
clippy::ptr_as_ptr,
clippy::borrow_as_ptr,
clippy::cast_ptr_alignment
)]

Expand Down Expand Up @@ -110,19 +108,19 @@ function generateWalkForStruct(type, types) {
if (field.wrappers.length === 2 && field.wrappers[1] === 'Vec') {
if (field.typeNameInner === 'Statement') {
// Special case for `Option<Vec<Statement>>`
walkCode = `walk_statements(traverser, field as *mut _, ctx);`;
walkCode = `walk_statements(traverser, std::ptr::from_mut(field), ctx);`;
} else {
walkCode = `
for item in field.iter_mut() {
${fieldWalkName}(traverser, item as *mut _, ctx);
${fieldWalkName}(traverser, std::ptr::from_mut(item), ctx);
}
`.trim();
}
} else if (field.wrappers.length === 2 && field.wrappers[1] === 'Box') {
walkCode = `${fieldWalkName}(traverser, (&mut **field) as *mut _, ctx);`;
walkCode = `${fieldWalkName}(traverser, std::ptr::from_mut(&mut **field), ctx);`;
} else {
assert(field.wrappers.length === 1, `Cannot handle struct field with type ${field.typeName}`);
walkCode = `${fieldWalkName}(traverser, field as *mut _, ctx);`;
walkCode = `${fieldWalkName}(traverser, std::ptr::from_mut(field), ctx);`;
}

return `
Expand All @@ -141,7 +139,7 @@ function generateWalkForStruct(type, types) {
// Special case for `Vec<Statement>`
walkVecCode = `walk_statements(traverser, ${fieldCode}, ctx);`
} else {
let walkCode = `${fieldWalkName}(traverser, item as *mut _, ctx);`,
let walkCode = `${fieldWalkName}(traverser, std::ptr::from_mut(item), ctx);`,
iterModifier = '';
if (field.wrappers.length === 2 && field.wrappers[1] === 'Option') {
iterModifier = '.flatten()';
Expand Down Expand Up @@ -169,7 +167,7 @@ function generateWalkForStruct(type, types) {
return `
${scopeCode}
${tagCode || retagCode}
${fieldWalkName}(traverser, (&mut **(${fieldCode})) as *mut _, ctx);
${fieldWalkName}(traverser, std::ptr::from_mut(&mut **(${fieldCode})), ctx);
`;
}

Expand Down Expand Up @@ -200,23 +198,23 @@ function generateWalkForStruct(type, types) {
}

function makeFieldCode(field) {
return `(node as *mut u8).add(ancestor::${field.offsetVarName}) as *mut ${field.typeName}`;
return `node.cast::<u8>().add(ancestor::${field.offsetVarName}).cast::<${field.typeName}>()`;
}

function generateWalkForEnum(type, types) {
const variantCodes = type.variants.map((variant) => {
const variantType = types[variant.innerTypeName];
assert(variantType, `Cannot handle enum variant with type: ${variant.type}`);

let nodeCode = 'node';
let nodeCode = '(node)';
if (variant.wrappers.length === 1 && variant.wrappers[0] === 'Box') {
nodeCode = '(&mut **node)';
} else {
assert(variant.wrappers.length === 0, `Cannot handle enum variant with type: ${variant.type}`);
}

return `${type.name}::${variant.name}(node) => `
+ `walk_${camelToSnake(variant.innerTypeName)}(traverser, ${nodeCode} as *mut _, ctx),`;
+ `walk_${camelToSnake(variant.innerTypeName)}(traverser, std::ptr::from_mut${nodeCode}, ctx),`;
});

const missingVariants = [];
Expand All @@ -239,7 +237,7 @@ function generateWalkForEnum(type, types) {

variantCodes.push(
`${variantMatches.join(' | ')} => `
+ `walk_${camelToSnake(inheritedTypeName)}(traverser, node as *mut _, ctx),`
+ `walk_${camelToSnake(inheritedTypeName)}(traverser, node.cast(), ctx),`
);
}

Expand Down
Loading