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

Add function name to the non local effect lint #1019

Merged
merged 6 commits into from
Jan 19, 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
31 changes: 27 additions & 4 deletions examples/general/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dylint_linting = { path = "../../../utils/linting" }

[dev-dependencies]
bitflags = "2.4"
derivative = "2.2.0"
once_cell = "1.19"

dylint_testing = { path = "../../../utils/testing" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,21 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalEffectBeforeErrorReturn {
|path, contributing_calls, span| {
// smoelius: The path is from a return to the start block.
for (i, &index) in path.iter().enumerate() {
let basic_block = &mir.basic_blocks[index];

if_chain! {
if !contributing_calls.contains(index);
if let Some(call_span) = is_call_with_mut_ref(cx, mir, &path[i..]);
if let Some((func, func_span)) = is_call_with_mut_ref(cx, mir, &path[i..]);
then {
span_lint_and_then(
cx,
NON_LOCAL_EFFECT_BEFORE_ERROR_RETURN,
call_span,
"call with mutable reference before error return",
func_span,
&format!("call to `{func:?}` with mutable reference before error return"),
error_note(span),
);
}
}

let basic_block = &mir.basic_blocks[index];
for statement in basic_block.statements.iter().rev() {
if let Some(assign_span) = is_deref_assign(statement) {
span_lint_and_then(
Expand Down Expand Up @@ -204,7 +203,7 @@ fn is_call_with_mut_ref<'tcx>(
cx: &LateContext<'tcx>,
mir: &'tcx Body<'tcx>,
path: &[BasicBlock],
) -> Option<Span> {
) -> Option<(&'tcx Operand<'tcx>, Span)> {
let index = path[0];
let basic_block = &mir[index];
let terminator = basic_block.terminator();
Expand All @@ -223,7 +222,7 @@ fn is_call_with_mut_ref<'tcx>(
if locals.iter().any(|local| is_mut_ref_arg(mir, local))
|| constants.iter().any(|constant| is_const_ref(constant));
then {
Some(*fn_span)
Some((func, *fn_span))
} else {
None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,13 @@ mod downcast {
Ok(())
}
}

use derivative::Derivative;

#[derive(Derivative)]
#[derivative(Debug)]
struct Foo {
foo: u8,
#[derivative(Debug = "ignore")]
bar: u8,
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | Err(VarError::NotPresent)
= note: `-D non-local-effect-before-error-return` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(non_local_effect_before_error_return)]`

error: call with mutable reference before error return
error: call to `std::vec::Vec::<u32>::push` with mutable reference before error return
--> $DIR/main.rs:28:8
|
LL | xs.push(0);
Expand All @@ -36,7 +36,7 @@ note: error is determined here
LL | let _ = var("X")?;
| ^^^^^^^^^

error: call with mutable reference before error return
error: call to `std::vec::Vec::<u32>::push` with mutable reference before error return
--> $DIR/main.rs:39:8
|
LL | xs.push(0);
Expand All @@ -60,7 +60,7 @@ note: error is determined here
LL | let result = Err(VarError::NotPresent);
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: call with mutable reference before error return
error: call to `std::vec::Vec::<u32>::push` with mutable reference before error return
--> $DIR/main.rs:64:8
|
LL | xs.push(0);
Expand All @@ -84,7 +84,7 @@ note: error is determined here
LL | match result {
| ^^^^^^^^^^^^

error: call with mutable reference before error return
error: call to `std::vec::Vec::<u32>::push` with mutable reference before error return
--> $DIR/main.rs:106:16
|
LL | xs.push(0);
Expand Down Expand Up @@ -120,7 +120,7 @@ note: error is determined here
LL | Err(Error::Two)
| ^^^^^^^^^^^^^^^

error: call with mutable reference before error return
error: call to `bitflags::_::<impl bitflags::Flags>::insert` with mutable reference before error return
--> $DIR/main.rs:185:15
|
LL | flags.insert(flag);
Expand All @@ -132,7 +132,7 @@ note: error is determined here
LL | return Err(());
| ^^^^^^^

error: call with mutable reference before error return
error: call to `std::string::String::push` with mutable reference before error return
--> $DIR/main.rs:202:11
|
LL | s.push('x');
Expand All @@ -144,7 +144,7 @@ note: error is determined here
LL | Err(())
| ^^^^^^^

error: call with mutable reference before error return
error: call to `std::process::Command::env::<&str, &str>` with mutable reference before error return
--> $DIR/main.rs:212:10
|
LL | .env("RUST_LOG", "debug")
Expand All @@ -156,5 +156,11 @@ error: assignment to dereference before error return
LL | *flag = true;
| ^^^^^^^^^^^^

error: aborting due to 14 previous errors
error: call to `std::fmt::DebugStruct::<'_, '_>::field` with mutable reference before error return
--> $DIR/main.rs:261:8
|
LL | struct Foo {
| ^^^

error: aborting due to 15 previous errors