Skip to content

Commit

Permalink
add Warn if no fields are given in a record update
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgleam committed Aug 6, 2020
1 parent 91c1db6 commit 4c0e24a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/typ/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ pub enum Warning {
Todo { location: SrcSpan },

ImplicitlyDiscardedResult { location: SrcSpan },

NoFieldsRecordUpdate { location: SrcSpan },
}

#[derive(Debug, PartialEq)]
Expand Down
8 changes: 8 additions & 0 deletions src/typ/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,14 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
)
.collect::<Result<_, _>>()?;

if args.is_empty() {
self.environment
.warnings
.push(Warning::NoFieldsRecordUpdate {
location: location.clone(),
});
}

return Ok(TypedExpr::RecordUpdate {
location,
typ: spread.typ(),
Expand Down
29 changes: 29 additions & 0 deletions src/typ/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2952,6 +2952,35 @@ fn main() { foo(); 5 }",
fn foo() { Ok(5) }
fn main() { let _ = foo(); 5 }",
);

// Some fields are given in a record update do not emit warnings
assert_no_warnings!(
"
pub type Person {
Person(name: String, age: Int)
};
pub fn update_person() {
let past = Person(\"Quinn\", 27)
let present = Person(..past, name: \"Santi\")
present
}",
);

// No fields are given in a record update emit warnings
assert_warning!(
"
pub type Person {
Person(name: String, age: Int)
};
pub fn update_person() {
let past = Person(\"Quinn\", 27)
let present = Person(..past)
present
}",
Warning::NoFieldsRecordUpdate {
location: SrcSpan { start: 183, end: 197 }
}
);
}

fn env_types_with(things: &[&str]) -> Vec<String> {
Expand Down
11 changes: 11 additions & 0 deletions src/warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ impl Warning {
write(buffer, diagnostic, Severity::Warning);
writeln!(buffer, "The Result value returned by this code is not being used, so any error is being silently ignored. Check for an error with a case statement, or assign it to the variable _ if you are sure the error does not matter.").unwrap();
}
NoFieldsRecordUpdate { location } => {
let diagnostic = Diagnostic {
title: "No fields are given in a record update".to_string(),
label: "".to_string(),
file: path.to_str().unwrap().to_string(),
src: src.to_string(),
location: location.clone(),
};
write(buffer, diagnostic, Severity::Warning);
writeln!(buffer, "The Record Update in this code is no fields are given.").unwrap();
}
},
}
}
Expand Down

0 comments on commit 4c0e24a

Please sign in to comment.