diff --git a/diesel/src/mysql/connection/raw.rs b/diesel/src/mysql/connection/raw.rs index 697414840358..fed7a67dd761 100644 --- a/diesel/src/mysql/connection/raw.rs +++ b/diesel/src/mysql/connection/raw.rs @@ -165,8 +165,8 @@ impl RawConnection { } fn consume_current_result(&self) -> QueryResult<()> { + let res = unsafe { ffi::mysql_store_result(self.0.as_ptr()) }; unsafe { - let res = ffi::mysql_store_result(self.0.as_ptr()); if !res.is_null() { ffi::mysql_free_result(res); } diff --git a/diesel/src/mysql/connection/stmt/metadata.rs b/diesel/src/mysql/connection/stmt/metadata.rs index 78bc0cac761a..b4f5c377cbb5 100644 --- a/diesel/src/mysql/connection/stmt/metadata.rs +++ b/diesel/src/mysql/connection/stmt/metadata.rs @@ -15,15 +15,13 @@ impl StatementMetadata { } pub(in crate::mysql::connection) fn fields(&'_ self) -> &'_ [MysqlFieldMetadata<'_>] { - unsafe { - let num_fields = ffi::mysql_num_fields(self.result.as_ptr()); - let field_ptr = ffi::mysql_fetch_fields(self.result.as_ptr()); + let num_fields = unsafe { ffi::mysql_num_fields(self.result.as_ptr()) }; + let field_ptr = unsafe { ffi::mysql_fetch_fields(self.result.as_ptr()) }; if field_ptr.is_null() { &[] } else { - slice::from_raw_parts(field_ptr as _, num_fields as usize) + unsafe { slice::from_raw_parts(field_ptr as _, num_fields as usize) } } - } } } diff --git a/diesel/src/mysql/types/date_and_time.rs b/diesel/src/mysql/types/date_and_time.rs index 7479a689a41e..c65f00824cee 100644 --- a/diesel/src/mysql/types/date_and_time.rs +++ b/diesel/src/mysql/types/date_and_time.rs @@ -113,8 +113,8 @@ macro_rules! mysql_time_impls { #[cfg(feature = "mysql_backend")] impl ToSql<$ty, Mysql> for MysqlTime { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result { + let bytes_ptr = self as *const MysqlTime as *const u8; let bytes = unsafe { - let bytes_ptr = self as *const MysqlTime as *const u8; slice::from_raw_parts(bytes_ptr, mem::size_of::()) }; out.write_all(bytes)?; diff --git a/diesel/src/pg/connection/raw.rs b/diesel/src/pg/connection/raw.rs index f13a6e7555da..43c6952d3e43 100644 --- a/diesel/src/pg/connection/raw.rs +++ b/diesel/src/pg/connection/raw.rs @@ -145,11 +145,9 @@ impl Drop for RawConnection { } fn last_error_message(conn: *const PGconn) -> String { - unsafe { - let error_ptr = PQerrorMessage(conn); - let bytes = CStr::from_ptr(error_ptr).to_bytes(); - String::from_utf8_lossy(bytes).to_string() - } + let error_ptr = unsafe { PQerrorMessage(conn) }; + let bytes = unsafe { CStr::from_ptr(error_ptr).to_bytes() }; + String::from_utf8_lossy(bytes).to_string() } /// Internal wrapper around a `*mut PGresult` which is known to be not-null, and diff --git a/diesel/src/pg/connection/result.rs b/diesel/src/pg/connection/result.rs index f751b861ad76..f02d91a73ae8 100644 --- a/diesel/src/pg/connection/result.rs +++ b/diesel/src/pg/connection/result.rs @@ -80,19 +80,17 @@ impl PgResult { } pub(super) fn rows_affected(&self) -> usize { - unsafe { - let count_char_ptr = PQcmdTuples(self.internal_result.as_ptr()); - let count_bytes = CStr::from_ptr(count_char_ptr).to_bytes(); + let count_char_ptr = unsafe { PQcmdTuples(self.internal_result.as_ptr()) }; + let count_bytes = unsafe { CStr::from_ptr(count_char_ptr).to_bytes() }; // Using from_utf8_unchecked is ok here because, we've set the // client encoding to utf8 - let count_str = str::from_utf8_unchecked(count_bytes); + let count_str = unsafe { str::from_utf8_unchecked(count_bytes) }; match count_str { "" => 0, _ => count_str .parse() .expect("Error parsing `rows_affected` as integer value"), } - } } pub(super) fn num_rows(&self) -> usize { diff --git a/diesel/src/sqlite/connection/raw.rs b/diesel/src/sqlite/connection/raw.rs index d7571b58f8f0..65d8e6afbab7 100644 --- a/diesel/src/sqlite/connection/raw.rs +++ b/diesel/src/sqlite/connection/raw.rs @@ -403,11 +403,11 @@ where ffi::sqlite3_aggregate_context(ctx, std::mem::size_of::>() as i32) }; let aggregate_context = NonNull::new(aggregate_context as *mut OptionalAggregator); - let aggregator = unsafe { - match aggregate_context.map(|a| &mut *a.as_ptr()) { + let aggregator = + match unsafe { aggregate_context.map(|a| &mut *a.as_ptr()) } { Some(&mut OptionalAggregator::Some(ref mut agg)) => agg, Some(a_ptr @ &mut OptionalAggregator::None) => { - ptr::write_unaligned(a_ptr as *mut _, OptionalAggregator::Some(A::default())); + unsafe { ptr::write_unaligned(a_ptr as *mut _, OptionalAggregator::Some(A::default())) }; if let OptionalAggregator::Some(ref mut agg) = a_ptr { agg } else { @@ -417,8 +417,7 @@ where None => { return Err(SqliteCallbackError::Abort(NULL_AG_CTX_ERR)); } - } - }; + }; let args = build_sql_function_args::(args)?; aggregator.step(args);