chore(lib): apply clippy fixes

This commit is contained in:
Amaan Qureshi 2024-02-04 01:30:46 -05:00
parent 04ff704bca
commit 28bb2a8c1c
No known key found for this signature in database
GPG key ID: E67890ADC4227273
4 changed files with 303 additions and 172 deletions

View file

@ -9,10 +9,7 @@ fn main() {
let scan_build_path = scan_build_path.to_str().unwrap();
env::set_var(
"CC",
&format!(
"{} -analyze-headers --use-analyzer={} cc",
scan_build_path, clang_path
),
format!("{scan_build_path} -analyze-headers --use-analyzer={clang_path} cc",),
);
}
}
@ -28,7 +25,7 @@ fn main() {
}
let src_path = Path::new("src");
for entry in fs::read_dir(&src_path).unwrap() {
for entry in fs::read_dir(src_path).unwrap() {
let entry = entry.unwrap();
let path = src_path.join(entry.file_name());
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
@ -80,9 +77,9 @@ fn generate_bindings() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let bindings_rs = out_dir.join("bindings.rs");
bindings.write_to_file(&bindings_rs).expect(&*format!(
"Failed to write bindings into path: {bindings_rs:?}"
));
bindings
.write_to_file(&bindings_rs)
.unwrap_or_else(|_| panic!("Failed to write bindings into path: {bindings_rs:?}"));
}
fn which(exe_name: impl AsRef<Path>) -> Option<PathBuf> {

View file

@ -23,11 +23,13 @@ impl Language {
/// # Safety
///
/// `ptr` must be non-null.
pub unsafe fn from_raw(ptr: *const TSLanguage) -> Language {
Language(ptr)
#[must_use]
pub const unsafe fn from_raw(ptr: *const TSLanguage) -> Self {
Self(ptr)
}
/// Consumes the [`Language`], returning a raw pointer to the underlying C structure.
#[must_use]
pub fn into_raw(self) -> *const TSLanguage {
ManuallyDrop::new(self).0
}
@ -39,8 +41,9 @@ impl Parser {
/// # Safety
///
/// `ptr` must be non-null.
pub unsafe fn from_raw(ptr: *mut TSParser) -> Parser {
Parser(NonNull::new_unchecked(ptr))
#[must_use]
pub const unsafe fn from_raw(ptr: *mut TSParser) -> Self {
Self(NonNull::new_unchecked(ptr))
}
/// Consumes the [`Parser`], returning a raw pointer to the underlying C structure.
@ -50,6 +53,7 @@ impl Parser {
/// It's a caller responsibility to adjust parser's state
/// like disable logging or dot graphs printing if this
/// may cause issues like use after free.
#[must_use]
pub fn into_raw(self) -> *mut TSParser {
ManuallyDrop::new(self).0.as_ptr()
}
@ -61,11 +65,13 @@ impl Tree {
/// # Safety
///
/// `ptr` must be non-null.
pub unsafe fn from_raw(ptr: *mut TSTree) -> Tree {
Tree(NonNull::new_unchecked(ptr))
#[must_use]
pub const unsafe fn from_raw(ptr: *mut TSTree) -> Self {
Self(NonNull::new_unchecked(ptr))
}
/// Consumes the [`Tree`], returning a raw pointer to the underlying C structure.
#[must_use]
pub fn into_raw(self) -> *mut TSTree {
ManuallyDrop::new(self).0.as_ptr()
}
@ -77,11 +83,13 @@ impl<'tree> Node<'tree> {
/// # Safety
///
/// `ptr` must be non-null.
pub unsafe fn from_raw(raw: TSNode) -> Node<'tree> {
Node(raw, PhantomData)
#[must_use]
pub const unsafe fn from_raw(raw: TSNode) -> Node<'tree> {
Self(raw, PhantomData)
}
/// Consumes the [`Node`], returning a raw pointer to the underlying C structure.
#[must_use]
pub fn into_raw(self) -> TSNode {
ManuallyDrop::new(self).0
}
@ -93,11 +101,13 @@ impl<'a> TreeCursor<'a> {
/// # Safety
///
/// `ptr` must be non-null.
pub unsafe fn from_raw(raw: TSTreeCursor) -> TreeCursor<'a> {
TreeCursor(raw, PhantomData)
#[must_use]
pub const unsafe fn from_raw(raw: TSTreeCursor) -> TreeCursor<'a> {
Self(raw, PhantomData)
}
/// Consumes the [`TreeCursor`], returning a raw pointer to the underlying C structure.
#[must_use]
pub fn into_raw(self) -> TSTreeCursor {
ManuallyDrop::new(self).0
}
@ -109,11 +119,12 @@ impl Query {
/// # Safety
///
/// `ptr` must be non-null.
pub unsafe fn from_raw(ptr: *mut TSQuery, source: &str) -> Result<Query, QueryError> {
Query::from_raw_parts(ptr, source)
pub unsafe fn from_raw(ptr: *mut TSQuery, source: &str) -> Result<Self, QueryError> {
Self::from_raw_parts(ptr, source)
}
/// Consumes the [`Query`], returning a raw pointer to the underlying C structure.
#[must_use]
pub fn into_raw(self) -> *mut TSQuery {
ManuallyDrop::new(self).ptr.as_ptr()
}
@ -125,13 +136,15 @@ impl QueryCursor {
/// # Safety
///
/// `ptr` must be non-null.
pub unsafe fn from_raw(ptr: *mut TSQueryCursor) -> QueryCursor {
QueryCursor {
#[must_use]
pub const unsafe fn from_raw(ptr: *mut TSQueryCursor) -> Self {
Self {
ptr: NonNull::new_unchecked(ptr),
}
}
/// Consumes the [`QueryCursor`], returning a raw pointer to the underlying C structure.
#[must_use]
pub fn into_raw(self) -> *mut TSQueryCursor {
ManuallyDrop::new(self).ptr.as_ptr()
}
@ -143,11 +156,13 @@ impl LookaheadIterator {
/// # Safety
///
/// `ptr` must be non-null.
pub unsafe fn from_raw(ptr: *mut TSLookaheadIterator) -> LookaheadIterator {
LookaheadIterator(NonNull::new_unchecked(ptr))
#[must_use]
pub const unsafe fn from_raw(ptr: *mut TSLookaheadIterator) -> Self {
Self(NonNull::new_unchecked(ptr))
}
/// Consumes the [`LookaheadIterator`], returning a raw pointer to the underlying C structure.
#[must_use]
pub fn into_raw(self) -> *mut TSLookaheadIterator {
ManuallyDrop::new(self).0.as_ptr()
}

File diff suppressed because it is too large Load diff

View file

@ -9,7 +9,7 @@ pub struct CBufferIter<T> {
}
impl<T> CBufferIter<T> {
pub unsafe fn new(ptr: *mut T, count: usize) -> Self {
pub const unsafe fn new(ptr: *mut T, count: usize) -> Self {
Self { ptr, count, i: 0 }
}
}
@ -23,7 +23,7 @@ impl<T: Copy> Iterator for CBufferIter<T> {
None
} else {
self.i += 1;
Some(unsafe { *self.ptr.offset(i as isize) })
Some(unsafe { *self.ptr.add(i) })
}
}
@ -38,7 +38,7 @@ impl<T: Copy> ExactSizeIterator for CBufferIter<T> {}
impl<T> Drop for CBufferIter<T> {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { (FREE_FN)(self.ptr as *mut c_void) };
unsafe { (FREE_FN)(self.ptr.cast::<c_void>()) };
}
}
}